Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 400
user2693017 Avatar asked Oct 20 '13 12:10

user2693017


People also ask

What is Lockfile in bash?

A basic lock file is when you create a file to be used as a lock. The existence of the file will be a signal to any subsequent instance of the script not to run. When the script exits, you delete the file. The next instance of the script will check for the existence of the file to determine if it is safe to execute.

What is a PID lock?

The PID file usually contains the process ID number of the already launched and running program if one exists. Also, when it starts up, it creates the lock file. As long as the lock file exists, it won't start another one without user intervention.


2 Answers

The PID is stored in $$

Like

echo $$ > thisscriptpidfile
like image 174
Aleks-Daniel Jakimenko-A. Avatar answered Oct 07 '22 18:10

Aleks-Daniel Jakimenko-A.


For any application, you can find its Process ID using the Unix shell itself, using ps. Example below is a very reduced list from ps. PS will show you not only the PID, but also the owner, as well as the Parent Process ID (as in which process started this particular process.)

userX#  ps -ef | more
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Oct19 ?        00:00:00 /sbin/init
root         2     0  0 Oct19 ?        00:00:00 [kthreadd]
root         3     2  0 Oct19 ?        00:00:02 [migration/0]
root         4     2  0 Oct19 ?        00:04:48 [ksoftirqd/0]
root         5     2  0 Oct19 ?        00:00:00 [migration/0]
root         6     2  0 Oct19 ?        00:00:00 [watchdog/0]
...
root        27     2  0 Oct19 ?        00:00:00 [pm]
root        28     2  0 Oct19 ?        00:00:00 rsnapshot
root        29     2  0 Oct19 ?        00:00:00 [xenbus]

Now let's start finding which Process is interesting to us. I am not familiar with rsnapshot, so I've put dummy data in the examples.

userX#   ps -ef | grep rsnapshot
root        28     2  0 Oct19 ?        00:00:00 rsnapshot
ec2-user  7233  1497  0 11:32 pts/0    00:00:00 grep rsnapshot

Note that it does not give you the "header" information, only matching lines, thanks to grep. Your second "column" is the PID. Noteworthy: ps shows every process, including the grep you just ran. Your commands/scripts need to be wary of this and strip out these items. I will use awk in the next example to do just that.

And now to expand further, getting the PID into a file. We need to confirm we have a PID, and if so, create the command to create the lock file:

userX#   ps -ef | grep rsnapshot |  awk '$0!~/grep/ && $2~/[0-9]/{print "echo "$2" > rsnapshot.lck"}'
echo 28 > rsnapshot.lck

If no PID for rsnapshot exists, then there will be no output. As writtent, awk will review each line, and if it does not contain the string "grep" AND there is any digit [0-9] in the second field, then print the command to be run - but not actually run the command.

The final step is to invoke the command, from the awk output.

userX#   ps -ef | grep rsnapshot |  awk '$0!~/grep/ && $2~/[0-9]/{print "echo "$2" > rsnapshot.lck"}' | sh

Adding "| sh" causes all output to be invoked as a command. If awk does not find rsnapshot, then there is no command to run.

like image 38
Bee Kay Avatar answered Oct 07 '22 20:10

Bee Kay