One instance bash script
Quick-and-dirty way to ensure only one instance of a bash script is running at a time, based on its name.
Suppose you have a bash script, for instance load_data_foo.sh, that is scheduled every ten minutes and you want to be sure that the only one instance of the script is running. Just use this code if you want a fast solution
#!/bin/bash
MY_FILENAME=`basename "$BASH_SOURCE"`
MY_PROCESS_COUNT=$(ps a -o pid,cmd | grep $MY_FILENAME | grep -v grep | grep -v $$ | wc -
l)
if [ $MY_PROCESS_COUNT -ne 0 ]; then
echo found another process
exit 0
if
# Follows the code to get the job done.
You can test it by adding a sleep 10 at the bottom of the script.