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

user2693017 picture user2693017 · Oct 20, 2013 · Viewed 24.7k times · Source

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 ?

Answer

Aleks-Daniel Jakimenko-A. picture Aleks-Daniel Jakimenko-A. · Oct 20, 2013

The PID is stored in $$

Like

echo $$ > thisscriptpidfile