mraza Posted December 2, 2010 Share Posted December 2, 2010 Hi, I wants to set a cron job to a script, now i am in confusion on how it will work, let me explain: Is that possible to do a cron job which will run when first task is completed, for example my cron is set to run after every 10 minutes but if my script is still running after 10 minutes , So this way it will interrupt the script as sometime my script take times to update and sometime it dont. So is that possible to make cron stop when script is still running and still keep it to like check after every 10 minutes for updates? Thanks for any help Quote Link to comment Share on other sites More sharing options...
tomfmason Posted December 2, 2010 Share Posted December 2, 2010 You could use the ps(process status) command in your cron script to check if the cron is still running from a previous task and exit if it is. That would seem like the easiest solution. Quote Link to comment Share on other sites More sharing options...
mraza Posted December 2, 2010 Author Share Posted December 2, 2010 Thank you sir for reply, is that possible for you to show me an example script please as i am not sure how i will accomplish this. currently i have set it like this in my kloxo cron settings: 10 * * * * /directory/script.php Thanks again. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted December 2, 2010 Share Posted December 2, 2010 ok here is an example that I use to check and see if one of my servers is running tomfmason$ ps aux |grep server.py 1001 2739 0.0 6.6 67136 17500 pts/5 Sl+ Nov23 3:30 /usr/bin/python /usr/bin/twistd -ny server.py 1001 16590 0.0 0.2 3904 540 pts/10 D+ 11:39 0:00 grep server.py The first result shown there is my server. Meaning that it is indeed running. From the cmd you would want to do something like this server$ ps aux |grep yourscript.php If that returns anything other than the second line from my example above your script is indeed running and you can simply exit the cron script. Quote Link to comment Share on other sites More sharing options...
mraza Posted December 2, 2010 Author Share Posted December 2, 2010 Thanks again sir, but where i will use this command? server$ ps aux |grep yourscript.php what my script does is check the rss feed of a particular site and keep me updated, now as i said above i have set that line after 10 minutes and run the script, still confuse how i will use your above command. should i add some kind of line in my yourscript.php at top like exec("server$ ps aux |grep yourscript.php", $command_output, $result); if ($result !== 0) { exit; } Thanks Quote Link to comment Share on other sites More sharing options...
tomfmason Posted December 2, 2010 Share Posted December 2, 2010 You would want to do something like this: <?php exec("ps aux |grep foo.php", $command_output, $result); $parts = explode(" ", $command_output[0]); if($parts[count($parts) -1] == "foo.php" && $parts[count($parts) -2] != "grep") { exit; } ?> You would obviously need to replace "foo.php" with the name of your cron script Quote Link to comment Share on other sites More sharing options...
mraza Posted December 2, 2010 Author Share Posted December 2, 2010 Thanks sir, i will do my tests but looks like this what i was looking Thanks again Quote Link to comment Share on other sites More sharing options...
tomfmason Posted December 2, 2010 Share Posted December 2, 2010 I just realized something. It will always exit with my example above since the script is checking if it is running from within it's self. An alternative would be to use this as a separate script that calls the actual cron script if it isn't running. For example: <?php exec("ps aux |grep foo.php", $command_output, $result); $parts = explode(" ", $command_output[0]); if($parts[count($parts) -1] == "foo.php" && $parts[count($parts) -2] != "grep") { exit; } else { exec("php /path/to/your/cronscript"); } Quote Link to comment Share on other sites More sharing options...
mraza Posted December 2, 2010 Author Share Posted December 2, 2010 thanks, so i will make a new script with above code cronset.php and put that code your provided and set a cron to this 10 * * * * /directory/cronset.php which will check my foo.php if running or not , alright thanks a lot again Quote Link to comment Share on other sites More sharing options...
steviewdr Posted December 5, 2010 Share Posted December 5, 2010 How about something as simple as the following in a crontab: ./script1.sh && ./script2.sh Script 2 will wait until script 1 finishes. -steve Quote Link to comment Share on other sites More sharing options...
mraza Posted December 15, 2010 Author Share Posted December 15, 2010 I just realized something. It will always exit with my example above since the script is checking if it is running from within it's self. An alternative would be to use this as a separate script that calls the actual cron script if it isn't running. For example: <?php exec("ps aux |grep foo.php", $command_output, $result); $parts = explode(" ", $command_output[0]); if($parts[count($parts) -1] == "foo.php" && $parts[count($parts) -2] != "grep") { exit; } else { exec("php /path/to/your/cronscript"); } helo sir today when i try to use above solution it always exit in my script. then i tried to run command in shell and this what i see [root@server /]# ps aux |grep myindex.php root 19637 0.0 0.0 1832 496 ttyp0 R+ 06:16 0:00 grep myindex.php [root@server /]# then i run myindex.php in browser and run above command again but it does not show if my file is running or not just one line as above. please any help? thanks How about something as simple as the following in a crontab: ./script1.sh && ./script2.sh Script 2 will wait until script 1 finishes. -steve i am not much familiar how i will do this crontab. thanks if u can explain this or above please regards Quote Link to comment Share on other sites More sharing options...
Mikehussy Posted December 18, 2010 Share Posted December 18, 2010 If there is an operation that is done once per day, make sure it is done when the system is expected to be on or bad things can occur. Years ago I worked on a SunOS system which was normally on only from 7am to 4pm (and properly shutdown at the end of the day) and rarely on outside that time period and the log file backup took place at 4AM on Sunday only. You can guess the result. edit: removed spamish links Quote Link to comment Share on other sites More sharing options...
recenttopnews Posted January 5, 2011 Share Posted January 5, 2011 Thanks tomfmason, I will follow your steps to set this action. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.