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>It is generally a good idea to check whether the application is running or not within the application rather than checking from outside and starting it. Managing the process within the process rather than expecting another process to do it.</p>

<ol>
<li>Let the cron run the application always</li>
<li>At the start of execution of the application, have a lock file or similar mechanism that will tell whether the process is already running or not. </li>
<li>If the application is already running, update somewhere the last test time and break the new process.</li>
<li>If the application is not running, log somewhere and notify someone (if required) and trigger the process start.</li>
</ol>

<p>This will ensure that you will have more control over the life time of a process AND let you decide what to do in case of failures.</p>

<p>Not to mention, if you want to make sure that the application up time is to be high, it is best to make use of monitoring services such as <a href="https://linux.die.net/man/1/monit" rel="nofollow">Monit</a>. Again this will depend on your application to add a sanity layer that says whether it is OK or not.</p>

python crontab