Scenario:
You are executing a background job for every x minuts periodically.The job execution time takes approximately y mins.Sometimes y > x. So, you are endup with multiple duplicate active process running at back.
Effets:
If duplicate process runs at back, it creates multiple side effect. The one is you would surprise to see the duplicate record in your table.
So, We can write a shell script which should check whether any active same process running or not? If not, spawn one.
Trick:
This trick might help to you.
#!/bin/bash
status=`ps -efww | grep -v grep |grep -w "python manager.py send_email" | awk -vpid=$$ '$2 != pid { print $2 }'`
echo $status
if [ ! -z "$status" ]; then
echo "Process already running"
else
cd /home/edge/projects/venv
source bin/activate
cd /home/edge/projects/taleo
python manager.py send_email
echo "[`date`] New Process Spawned"
fi
my process python manager.py send_email
will be grepped before it gets spawned.
your suggestions are welcome 🙂
Advertisements
#!/bin/bash
LOCKFILE=/var/run/cron-task.lock
set -e
(
flock -n 200
trap “rm $LOCKFILE” EXIT
mkdir -p -m 660 /home/d-metrius/google
… YOUR CODE COMES HERE …
) 200>$LOCKFILE
Pingback: 1 – Avoid duplicate process copy through cron | Exploding Ads