In Linux adding a lock file to a bash script I would say is a must. To often does the simplest of things turn into a major issue.
A very basic way of adding a lockfile to your bash script is like this.
#!/bin/bash - LOCK_FILE="/tmp/iama.lockfile" if [ ! -e $LOCK_FILE ]; then touch $LOCK_FILE #Add some command here
rm -f $LOCK_FILE else echo "Script is still running" fi
Following on from this if you want to use your bash script in a cronjob for instance and would like some notification
#!/bin/bash - MAIL_RECIPIENT="deademail@mrbuckykat.com" LOCK_FILE="/tmp/iama.lockfile.2" MAIL=/bin/mail
if [ ! -e $LOCK_FILE ]; then touch $LOCK_FILE # Add some command here say grep for errors in a log file
rm -f $LOCK_FILE
else echo "Script is still running" | $MAIL -s "!!WARNING!! Script is still running" $MAIL_RECIPIENT fi
Simple thing but can save alot of pain.
Enjoy.
1 Comment