Jump to content

setting cron job timing


mraza

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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  :confused:

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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");
}

Link to comment
Share on other sites

:) 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 :)

 

 

Link to comment
Share on other sites

  • 2 weeks later...

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.