5

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 ?

asked Oct 20, 2013 at 12:43
1
  • You could look at the lockfile command, it will help with your first step Commented May 7, 2014 at 6:34

2 Answers 2

8

The PID is stored in $$

Like

echo $$ > thisscriptpidfile
answered Oct 20, 2013 at 13:04
Sign up to request clarification or add additional context in comments.

Comments

2

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.

answered Oct 20, 2013 at 15:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.