Is it possible to reboot every 3 hours and also execute a script every 6 hours using Cron? - linux

Basically, this is my crontab
0 */3 * * * sleep 70 && touch /etc/banner && reboot
10 */6 * * * /root/updater
The first does a reboot and the second does an update. I want to check for updates every six hours, but reboot every 3 hours. I want them both to be independent.
I gave an offset of 10 minutes to the update script thinking it might compensate for the reboot time. Reboot time is about a minute on my Linux board.

Related

cron job from boot/reboot and every 10min after

despite reading many things on cron I am not able to achieve what I am looking for :
run a monitoring script every 10 minutes from the server start/reboot time.
I was able to make it work at 10min after a reboot/start with :
#reboot sleep 600 && bash ./monitoring.sh | wall
however I did not have the repetition after.
the other way I was able to do it was :
* * * * * sleep 600 && bash ./monitoring.sh | wall
but this would wait ten minutes and broadcast each minutes after.
if I change to :
*/10 * * * * sleep 600 && bash ./monitoring.sh | wall
it does wait 10 minutes after start/reboot, but then broadcast at each 40, 50 minutes.
Can it be done with cron or do I need to go the script way ?
thanks.

Using crontab to execute script between 17:00–20:00 for every 10 minutes

I want to send emails to clients every day between 17:00 to 20:00. I want to run my command every 10 minutes in this period.
So the script will be executed 6 times per hour. That's a total of 18 times.
Is this possible with the crontab? How should I write the syntax?
I think this should work:
0/10 17-19 * * * <cmd>
or:
0/10 17,18,19 * * * <cmd>

Wait 60 Seconds to Run Cron Job After Reboot Then Run Job Every 10 Minutes

I have a script that I would like to run 60 seconds after initial system reboot and then every 10 minutes after that. I currently need two cron job listings to achieve this:
*/10 * * * * php myscript.php
#reboot /bin/sleep 60; php myscript.php
The first listing will run my cron job immediately after system boot and so I need to have the second listing to account for the on start wait time.
Is there anyway to combine the above two cron listings into one?

Running a cron job randomly for every one hour

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.

How the cron timing is working?

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.

Resources