0

I've a script that build a pid file:

if [ -f $PIDDIR$NOS.pid ]
then
echo "PID exists"
exit 1
else
echo "PID does not exist"
echo
echo "build..."
touch $PIDDIR$NOS.pid
[starting script...]

I need "a check" that remove that file if (i.e.) the script has killed or other problems like disk full. Otherwise next execute of script does not run because there's the pid file.

2
  • Does an EXIT and/or ERR trap do what you want here? Alternatively improve your check to write the script pid to the file and then read and check that the pid is still alive and the script you expect in your check condition. Commented Nov 18, 2014 at 13:07
  • 1
    use trap. some simple tutorial here Commented Nov 18, 2014 at 13:09

1 Answer 1

1

A usual solution to this is, to not only rely on the existance of the file, but write the PID into it and check if the PID exists together with the file. If it does not, the PID is stale.

PID=$(cat program.pid)
if [ -e /proc/${PID} -a /proc/${PID}/exe -ef /usr/bin/program ]; then
   echo "Still running"
fi

(see here: http://www.odi.ch/weblog/posting.php?posting=291)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.