Cron job scheduling - cron

I have two Cron jobs in my web server -
*/5 0-3 * * 0-4 [my job]
*/5 19-23 * * 0-4 [my job]
We are in the GMT+6 timezone, and the server is in GMT-8 timezone. We would like the job to run -
Sunday to Friday
9 AM to 5 PM at GMT+6 timezone (office hours)
Every 5 minute interval
But it appears that the cron job runs on Fridays and also stops at Sunday noon at GMT+6 zone.

I'm not aware of any easy way to change the timezone. Some older versions of cron supported an environment variable called CRON_TZ, but the current version of cron doesn't seem to.
So you'd have to convert the times to system time.
I think you have calculated the times correctly, and the days correctly, but you have to take them together. For example, 5pm on Thursday at UTC+6 is 3am on Thursday at UTC-8, but your rules aren't running anything on Thursday.
By my calculations, you want:
Sunday 0900-1700 UTC+6 = Saturday 1900-2400 and Sunday 0000-0300 UTC-8
Monday 0900-1700 UTC+6 = Sunday 1900-2400 and Monday 0000-0300 UTC-8
...
Thursday 0900-1700 UTC+6 = Wednesday 1900-2400 and Thursday 0000-0300 UTC-8
Friday 0900-1700 UTC+6 = Thursday 1900-2400 and Friday 0000-0300 UTC-8
Which you would express in crontab as:
*/5 19-23 * * 0 [my job]
*/5 0-3,19-23 * * 1-4 [my job]
*/5 0-3 * * 5 [my job]
This solution doesn't feel that good, however.
What happens when daylight savings changes? Will you need to update the crontab?
Maybe if you have root access, you can modify the /etc/init.d/cron entry and put TZ=America/Los_Angeles or similar in there instead?
Of course, that solution sounds bad too, especially if other people use the system. Might as well just change the system's time zone if you're doing that.
Unfortunately there's not a lot of suggestions for alternatives to cron
alternative to cron?

This article helped me http://starikovs.com/2011/12/21/cron-timezone-problem/.
In case of debian linux, first start dpkg-reconfigure tzdata.
Link /etc/localtime to a corresponding zoneinfo file.
Restart cron daemon.

Related

how to run a cron job at a certain frequency starting from a specific date?

I tried using both npm packages "cron" and "node-schedule" to make this work, but i couldn't.
Let's say i want to start the cron job at 6.30PM, and starting from 6PM i want it to be executed every 2 hours.
First is that i couldn't make it work if it is not an exact hour , meaning if it is not (1PM,2PM,8PM.....)
So i supposed that it will run at 6PM sharp and not 6.30PM
I tried this pattern:
"0 18-23/2 * * *" which is supposed to work from 18 to 23, escaping two hours [18,20,22,00] but once it's past midnight it will stop til it's 18h again. i want it to keep executing every 2 hours (meaning it executes at 2AM and so on ...)
IS that possible with cron jobs ?
You could do a basic shell script to check if the current date and time are greater than or equal to the date and time you specify before executing the command.
Please note that the date and time are specified in UTC. This will cause the cron to run every 2 hours 30 minutes past the top of the hour but it wont actually activate until after 6PM December 3rd 2019 UTC. Also note that the date command may vary with different flavors of nix* / BSD / OSX.
30 */2 * * * if [ $(date --utc +%s) -ge $(date --utc --date "2019-12-03T18:00:00" +%s) ]; then (command) fi
I believe you are talking about using crontab to schedule jobs.
crontab is designed to let you set the frequency based on many things but it is not designed to become "enabled" a certain time.
so the following will run every two hours
0 0,2,4,6,8,10,12,14,16,18,20,22 * * * (command)
Lets say it is 1st of the month and you want it to run every two hours starting at 6 pm
I would do the following
0 18,20,22 1 * * (command)
0 0,2,4,6,8,10,12,14,16,18,20,22 2 * * (command)
This means that today (based on day of month it would run starting at 6 and tomorrow it will run every two hours. Then sometime tomorrow I would edit the file and remove the first line and change the '2' for the day of month to a '*' (to match the first line I showed you) -- now it will always run every two hours.
tldr; edit crontab to work for two days and then edit it after the go live to work for every day.

Complex MuleSoft Cron Schedule

I'm trying to schedule an app to run every 5 minutes, M-F from 6am-6pm, and every 2 hours, M-F from 6pm-6am. The fixed poll frequency doesn't allow this level of scheduling, so I'm trying to use a cron scheduler.
I set the time zone to America/Chicago and the 5M expression to 0 0/5 6-18 ? * 2-6, and I set the 2H expression to 0 0/120 18-23,0-6 ? * 2-6.
According to both Cron documentation and MuleSoft documentation, this should be setup correctly and should work, and it does work locally. When I publish this to our VPC (US-EAST), I found that the 2H scheduler was working during the day until about 4PM (Central time), so I changed the expressions to this:
5M 0 0/5 0-12 ? * 2-6
2H 0 0/120 12-23 ? * 2-6
Now my 5M scheduler started at 2:15AM and ran until 7AM.
How can I setup two central timezone cron schedules to run 6am-6pm M-F every 5 minutes and 6pm-6am M-F every 2 hours on a VPC server?
MuleSoft confirmed to me that their VPC servers are UTC based, regardless of waht time zone you place on the cron scheduler/poll connector. While not ideal, that means that I needed to back the schedulers up 6 hours (to get them to central time). I suspect that DST will cause the schedule to shift one direction by an hour.
This is a good resource for learning about Mule Quartz/Cron scheduler.
There is also a Free Online cron scheduler formatter.
For the purpose of my scheduling needs, I ended up needing a total of four cron schedulers
0 0/5 12-23 ? * 2-6 - runs M-F, 6am-5:55pm (central) every five minutes.
0 0 0-11/2 ? * 3-6 - runs T-F UTC, but actually runs 6pm-4am M-F every two hours. You only need to run the app up to 4am since it is every two hours, and the five minute schedule will start promptly at 6am. Two things to note here. First, to properly rune very two hours you need to do /2 on the hour slot, instead of /120 on the minute. The minute slot can only handle values from 0-59. Second, the days are set to start on Tuesday because of the 6 hour difference from UTC to Central. If you were to use hours 0-11 (UTC) on Monday, the app would actually start at 6pm on Sunday central time.
This brings us to the two additional schedules, one to capture 12am-4am on Monday (central), and a second to capture 6pm-12am on Friday (central).
0 0 6-11/2 ? * 2 - runs every two hours between 12am-4am (central) on Monday.
0 0 0-6/2 ? * 7 - runs every two hours between 6pm and 12am (central) on Friday.

Cron Expression - Start at 10:20 and execute each 10min until 19:00

I have a case in which I'm migrating some tasks from Windows to a platform and we are using cron expressions to replace the Windows Scheduler.
Today we have something in Windows like At 10:20 AM every weekday, every 10 minutes for 9 hours. I'm trying to replace it with chron but I couldn't achieve it so far.
The closest I got is 0 20/10 10-19 * * MON-FRI. The thing is on this cron, it won't execute at 11:00, 12:00 and so on. We have a specific case in which we don't want it to execute at 10:00 AM.
The only option I found is to execute at 10:00AM and put some condition to validate it. Is it possible to achieve this result with only chron?
Thanks!
You can do it with cron, but you'll need to break it up into two schedules.
20/10 10 * * MON-FRI
and
*/10 11-19 * * MON-FRI
Btw, if this is cron on unix, there is no field for seconds.

Run a cron job every minute only between 10 am to 5 pm

How do you run a cron job every minute only between office hours(10 am to 5pm)
I checked this thread Run a cron job every minute only on specific hours? but it doesn't answer my questions.
This should be correct:
* 10-16 * * 1-5 /path/to/my-script
So every single minute, between and including 10am and 5pm, every day in every month that is a day between and including monday to friday. Obviously "office hours" is a fuzzy expression, many people have other schedules ;-)
Unfortunately I fail to see an easy solution to get the script executed also exactly on 5pm...
* 10-16 * * * /path/to/executable/file argument_1 argument_2
Yes, you can define hours range.
Someone tried to edit my answer but as documentation says hours in range are inclusive http://team.macnn.com/drafts/crontab_defs.html so don't change 16 to 17.
It does,
Access your shell script and add the following
* 10-17 * * *
This means run every min, between these hours, on every day, of every month etc

Run cron at 7AM on multiple timezones

I would like to run cron jobs when it is 7AM locally in Western US, Eastern US and some other timezones in Asia and Europe.
This is a node.js server, so I can probably use node-cron to do this but I'd like to use the regular cron if it's at all possible.
Times in the crontab always refer to the current system time. You will have to subtract or add timezone differences to calculate the execution intervals on your server.
E.g.:
Your server is in the Western US (UTC+8) and the system date is set to be UTC+8. 7am can be specified in crontab using
0 7 * * * /run/me-for-western-us
To run a script when it is 7 am in Eastern US (UTC+5), you need to subtract 3 hours
0 4 * * * /run/me-for-eastern-us
Do the same for all other timezones.

Resources