crontab to run python file if not running already
<p>I want to execute my python file via crontab only if its down or not running already. I tried adding below entry in cron tab but it does not work</p>

<pre><code>24 07 * * * pgrep -f test.py || nohup python /home/dp/script/test.py &amp; &gt; /var/tmp/test.out
</code></pre>

<p>test.py works fine if i run  <code>pgrep -f test.py || nohup python /home/dp/script/test.py &amp; &gt; /var/tmp/test.out</code> manually and it also works in crontab if i remove pgrep -f test.py || from my crontab and just keep <code>24 07 * * * nohup python /home/dp/script/test.py &amp; &gt; /var/tmp/test.out</code></p>

<p>Any idea why crontab does not work if i add pgrep -f? is there any other way i can run test.py just one time to avoid multiple running processes of test.py?
Thanks,
Deepak</p>

<p>cron may be seeing the <code>pgrep -f test.py</code> process as well as the <code>test.py</code> process, giving you the wrong result from <code>pgrep</code>.<br>
Try the command without the <code>-f</code>, this should just look for an occurrence of <code>test.py</code> or replace <code>-f</code> with <code>-o</code> which will look for the oldest occurrence.</p>

<p>Your other option is to insert into test.py something along the lines of:</p>

<pre><code>Pid = os.popen('pgrep -f test.py').read(5).strip()
</code></pre>

<p>which will allow you to check within the code itself, if it is already running.</p>

python crontab