How can i set three cron jobs for every two days interval but not runs all.
This is is my three cron job process:
0 20 */2 * * /usr/local/bin/ruby /var/www/html/data_download1.rb >/dev/null 2>&1
0 20 */2 * * /usr/local/bin/ruby /var/www/html/data_download2.rb >/dev/null 2>&1
0 20 */2 * * /usr/local/bin/ruby /var/www/html/data_download3.rb >/dev/null 2>&1
You can use 1-31/2 for one task and 2-30/2 for the other to specify alternating days. This will fail on months with odd numbers of days as the 1-31/2 job will run twice in a row.
If you absolutely must have it right, you can check whether the number of days since a particular date is odd or even and have one script abort if it's odd and the other abort if it's even.
Related
I need to run two cronjobs. One is to run in every 20 minutes every day from 1AM to midnight. Then the second job is scheduled to run at midnight.
*/20 1-23 * * * root [job1]
0 0 * * * root [joob2]
However, I need to make sure that job 2 is completed when job 1 is run again.
How can I do this?
I think this is what you might be after
*/20 1-23 * * * root while [ ! -e $HOME/jobmarker ]; do sleep 5; done && command_job1
0 0 * * * root rm $HOME/jobmarker && command_job2 && touch $HOME/jobmarker
This will only run job1 if the file $HOME/jobmarker exists. If not, it will wait until it is generated.
The second job will first remove the marker, run the command and then set the marker again.
A simple solution is to make job 2 create a flag file (eg touch) when starting, which it will remove when it's completed.
Then job11 should check if the file exists and make it exit / don't run if it exists. Or sleep for a specified amount of time and then try again.
I want a cronjob to run every one hour randomly. (i.e if the first job runs at 58 minutes,the second job should run at 47 minutes and the third one at 52 minutes and so on) But this should run randomly for everyone hour. Is there a way to do this?
You could run a job every hour, on the hour, that sleeps up to 3,599 seconds and then executes your script:
0 * * * * /path/to/perl -e 'sleep int rand 3600' && /path/to/yourScript
Or, using PHP if you prefer that to Perl:
0 * * * * /path/to/php -r 'sleep(rand(0,3599));' && /path/to/yourScript
You can find the path to Perl with:
which perl
likewise for PHP:
which php
Instead of using perl or even php, just use the BASH $RANDOM built in divided by 3600 which equals one hour like so.
0 * * * * sleep $((RANDOM%3600)) && /path/to/yourScript
Keep in mind that you will probably have some race conditions with a script sleeps randomly close to an hour depending on how long it takes for your script to execute.
Suppose, current time is 11:42 and i have setup one cron file to run at every 5 minutes.
Then this file will run at which time 11:47 or 11:45?
So basically i am trying to understand that how the cron timing is work?
Edit : it was ran at 11:45, but i don't know the reason behind it
Cron Configuration :
*/5 * * * * wget -O /dev/null http://XXX/index.php?r=controller/action
As you know, cron will run jobs at a specific time.
A cron job will not use the time it was started, only the configuration matters. This means a cron job set to every 5 minutes (like your */5 * * * *) will only ever run at times ending with 0 or 5 (eg: 12:00, 12:05, 12:10), regardless of the time you run it. This makes sense because we want to schedule a job for a specific time.
If you really need a job to run every 5 minutes, with an offset (eg: 11:42, 11:47, 11:52) you will have to give a list in the configuration.
instead of (*/5 * * * *) you would need to use:
(2,7,12,...,57 * * * *), filling ... with all the other numbers.
i have two cronjobs, i want to run the two cronjobs at a small gap of ten minutes. once in two days, at 0100 hours and 0110 hours
this is what iam trying.
0 1 */2 * * job1.sh
10 1 */2 * * job2.sh
job1 is not working as expected. it runs twice everyday.
job2 runs as expected (once in two days).
what am i doing wrong?
You need to add the binary executing the script, as well as the complete route to your file:
0 1 */2 * * /bin/sh /route/to/your/file/job1.sh
10 1 */2 * * /bin/sh /route/to/your/file/job2.sh
/bin/sh can be another thing, just get it from which sh.
I want to set a cron run after an other cron. For example: Cron A finishs at 01:00 PM, cron B will start at 01:01 PM. The problem is I don't know when cron A finishs.
I checked the crontab syntax. It doesn't provide any param for that purpose.
My actual situation is:
# This cron must run first.
? ? * * * /usr/local/bin/php -f /path/select_and_print_to_log_file.php
# two these crons runs at the same time.
0 13 * * * /usr/local/bin/php -f /path/update_user.php
0 13 * * * /usr/local/bin/php -f /path/update_image.php
# This cron runs right after two above cron completes.
? ? * * * /usr/local/bin/php -f /path/select_and_print_to_log_file.php
You can use the batch command inside the first cron to have the second thing being scheduled to run.
Your first job could produce a timestamp when finished.
Then you estimate - for example - that job A needs about 60 to 90 minutes. After 60 minutes, you start job B. Job b looks for the timestamp. If it is present, job B starts, else it waits for a minute and looks again.
After finishing, job B deletes the timestamp, or renames it, maybe from 'todo' to 'done'. You could insert the current date inside the file, to check, whether your estimation is still acceptable, or should be adjusted.
What I do in such cases (commonly a backup scenario where I don't want to thrash the disk by having concurrent backups) is to write a script that cron calls, and in the script have the actual tasks run serially.
Something like:
#!/bin/bash
/usr/local/bin/php -f /path/update_user.php
/usr/local/bin/someOtherTaskToRunSecond
YMMV.