Skip to content

Instantly share code, notes, and snippets.

@mmccall10
Created October 4, 2018 15:34
Show Gist options
  • Save mmccall10/a2cb684ca1e220e241d5349ddb8cace1 to your computer and use it in GitHub Desktop.
Save mmccall10/a2cb684ca1e220e241d5349ddb8cace1 to your computer and use it in GitHub Desktop.
Bash script to run other scripts uniquely
#!/bin/bash
PIDFILE=$2
SCRIPT=$1
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
$SCRIPT
rm $PIDFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment