Avoid duplicate process copy through cron


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.


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.


This trick might help to you.


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"
    cd /home/edge/projects/venv
    source bin/activate

    cd /home/edge/projects/taleo
    python manager.py send_email

    echo "[`date`] New Process Spawned"

my process python manager.py send_email will be grepped before it gets spawned.

your suggestions are welcome 🙂

About Navaneethan

mixed feelings...
This entry was posted in 10min, Flask, Python and tagged , , . Bookmark the permalink.

2 Responses to Avoid duplicate process copy through cron

  1. #!/bin/bash


    set -e
    flock -n 200
    trap “rm $LOCKFILE” EXIT
    mkdir -p -m 660 /home/d-metrius/google
    ) 200>$LOCKFILE

  2. Pingback: 1 – Avoid duplicate process copy through cron | Exploding Ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s