How can make cron job which happen at different hours and minutes - linux

I want to make crontab where script occurs at different minutes for each hour like this
35 1,8,12,15,31 16,18,21 * * 0,1,2,3,4,5,6 python backup.py
I want script to run at 16hour and 31 minutes but it is giving me error bad hour
i want the cron occur at
1:35am , then 16:31, then 21:45

As there is not a pattern that can match the three times, it is not possible to schedule that just with one crontab expression. You will have to use three:
45 21 * * * python backup.py
31 16 * * * python backup.py
35 1 * * * python backup.py
Note also that python backup.py will probably not work. You have to define full path for both files and binaries:
35 1 * * * /usr/bin/python /your/dir/backup.py
Where /usr/bin/python or similar can be obtained with which python.

If the system which you are on has systemd, You can look into systemd timers(https://www.freedesktop.org/software/systemd/man/systemd.time.html). Then you might be able to achieve the randomness using the RandomizedDelaySec setting and an OnCalendar setting which will schedule the service to run every hour or interval you set plus will generate a RandomizedDelaySec at every run so that the interval is random.

Related

Created cron job to run every 2 mint

I have configured cron job but it's not working.
I wanted to run the myfile.sh script for every 2 mint and below are my configuration in crontab.
# m h dom mon dow comman
2 * * * * /home/ubuntu/myfile.sh
myfile.sh is executable and contains below lines of code
#!/bin/bash
mysqldump -u[user] -p[password] --single-transaction --routines --triggers --all-databases > /home/ubuntu/backup_db10.sql
Is there anywhere we need to add configure anything?
You're running the script at two minutes past every hour. As in 1:02, 2:02 and so on.
You can change it to something like
*/2 * * * * /home/ubuntu/myfile.sh
to run it every two minutes.
A bit more info can be found here.

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>

Set Cron Job Between Specific Times and Frequency

For example I want to run job every 6 minutes between;
16:34 - 18:45
So it must be running on 16:34, 16:40, 16:46 etc. to 18:40.
When I write
34-45 16-18 * * *
It only works between 16:34-16:45 and 18:34-18:45. But I don't want this one.
Is it possible to make this?
Thank you
Hm, where in your cron line is the "every 6 minutes" part? Also, why would your cron go to 18:40, wouldn't 18:42 be the last time you want it to run? Let me know if I'm not understanding the question correctly.
Anyway though, not sure if it's possible in one cron line, but you could always do something like:
34/6 16 * * *
*/6 17 * * *
0,6,12,18,24,30,36,42 18 * * *
Edit: Or, if you have control and are able to edit the file/executable your cron is running you could do:
*/6 16-18 * * * /path/to/myScript
And then at the very beginning of myScript:
if time < 16:34 or time > 18:45:
exit # kill script

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.

Cron schedule - Execute job at three different times with no pattern between. Is it possible?

I have a job I want to run using Quartz, which require the time to be specified using a Cron schedule.
The job should run at 17:00, 17:35 and 22:05.
Is it possible to specify these times using a Cron schedule ? I've already looked into the Wikipedia article, but it did not help much. From my point of view, there needs to be a pattern between the times, so that you can specify them using a Cron schedule.
Best regards
Nicolas
You can do it with 3 cron jobs. example :
> crontab -e
00 17 * * * sh /example/script.sh
35 17 * * * sh /example/script.sh
05 22 * * * sh /example/script.sh

Resources