Search code examples
linuxbashshellpidlockfile

Get the pid of the shell script and save it into a lockfile


I do my backups with rsnapshot which creates a lockfile with the process pid inside. Now I would like to make a backup from the rsnapshots backup, so I´m looking for a way to create this lockfile for the second/external backup.

The shell script should like this:

  1. check if a lockfile exists, if yes wait and try again(i´m doing this with a while true loop)
  2. get the pid of this shell script and save it as a rsnapshot lockfile
  3. start the second/external backup
  4. delete the lockfile

How can I get the PID and save it as a rsnapshot lockfile ?


Solution

  • The PID is stored in $$

    Like

    echo $$ > thisscriptpidfile