Skip to content

Instantly share code, notes, and snippets.

@moofish32
Created March 25, 2017 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moofish32/7cfd46f43b26f776dd11dd48fe61c865 to your computer and use it in GitHub Desktop.
Save moofish32/7cfd46f43b26f776dd11dd48fe61c865 to your computer and use it in GitHub Desktop.
Adding the above rm command to the end of the script will result in cleaning up the PID file after a successful execution. If the script exits any other places this step should be included before the exit command.
Summary of Changes
Now that we have made our changes let's take another look at this script.
#!/bin/bash
PIDFILE=/home/vagrant/forever.pid
if [ -f $PIDFILE ]
then
PID=$(cat $PIDFILE)
ps -p $PID > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "Process already running"
exit 1
else
## Process not found assume not running
echo $$ > $PIDFILE
if [ $? -ne 0 ]
then
echo "Could not create PID file"
exit 1
fi
fi
else
echo $$ > $PIDFILE
if [ $? -ne 0 ]
then
echo "Could not create PID file"
exit 1
fi
fi
sleep 25d
rm $PIDFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment