Avoid duplicate process copy through cron

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

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

    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

  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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s