Run cron job every 2 days on specified hour - cron

How can I run a cronjob for every 2 days on a specified hour like 4:10 AM?
Is the following expression right?
10 04 * * */2 MY-COMMAND

No, you are running on days of the week which are evenly divisible by two. So you are running it on Sun, Tue, Thu, Sat; Sun, Tue, ... (This field is zero-based.)
If you move the */2 to the month field instead (fourth field), you get the same problem with uneven periodicity in months with an uneven number of days, but the aberrant days will happen only seven times a year (eight in leap years) instead of every week.
If you absolutely require the job to run every other day, you need some kind of external logic. Maybe make the script check a run file, and abort if it's less than 25 hours old (or maybe 26 if you change the system time for daylight saving time) and otherwise proceed and update the time stamp of the run file.

If you look at the job execution days you can find that it also depends on the month that you are using (ex) if you are trying to execute every 5 day then the job will be start and it will try to split the month in equal halfs (which is not) and thus it wont work as expected seeClick Here to check so it becomes mandatory to use the withIntervalInHours(intervalInHours) to get our case working

Related

How to set time to 2 days after the current time with shell script in Solaris?

I would like to modify the system time to two days after but I only find that I can set the time to a specific time with the date command in Solaris:
#date mmddhhmmYY
but cannot add two days on the current time. Is there an easy way to do this task with a shell command? If you know something similar in linux, please also share with me.
Solaris date supports a syntax that gradually adjusts the time. This may be your best bet.
date -a $(( 48 * 60 * 60 * 60 ))
should eventually update your date to two days ahead. That is really the best way to set the system time as it will gradually update instead of jumping ahead suddenly (which can screw up a bunch of running programs).
Barring that, you could write up a nice script that's aware of the days in a month, and leap years, and do the calculations yourself. If you're exceptionally lazy (like me) and precision and race conditions don't bug you that much (like me, answering StackOverflow questions), you could just do a hack like this:
#!/bin/sh
now=$(date +%H%M.%S) # current time
date 2359.59 # Set time to 11:59:59pm
sleep 1 # Wait a second so the day rolls over
date 2359.59 # Set time to 11:59:59pm
sleep 1 # Wait a second so the day rolls over
date "$now" # Set time back to what it was, but in the new day
date -a 2 # Gradually add two seconds back to the clock
Test run on a tnarik/solaris10-minimal Vagrant box:
# ./adddaystodate
The current date is now Monday, 25 January 2016 00:46:59 GMT
Monday, 25 January 2016 23:59:59 GMT
Tuesday, 26 January 2016 23:59:59 GMT
Wednesday, 27 January 2016 00:46:59 GMT
The current date is now Wednesday, 27 January 2016 00:46:59 GMT

cron expression for quartz to start on particular month and repeat every months onwards

I am working on Quartz Scheduler where need to trigger my job on basis of monthly where user can select desired month date from which he want to make it run for every month on that particular date. lets say- I want to schedule quartz job from August 20,2015 which should run every month on 20th, but it should not start by today,must be start on August 20,2015 onward. what would be the cron expression for this?
I have tried a lot to find out the matching thread but did not worked for me.
like if i have to make for every month which start on 20 May 2015 and repeat every 20th date of month the cron expression would be [0 0 12 20 1/1 ? *].for this requirement lot of things available around and works nicely. but how to schedule Quartz which must fire on particular date and repeat onward for every month on that particular date and time?
Please help me out.any link or any guideline would be appreciable.

Schedule cron job to run on daylight savings time

I have a requirement to run one or two cron jobs (if one is not enough) for the day light savings every year. The script should be executed every year at below timings.
1) 2:00 am on second sunday of march.
2) 2:00 am first sunday of november.
I could make it to run every sunday of a month, is there a way to make it work for a specific day like this?
No, crontab has no syntax for this.
What you can do is schedule a job to run every Sunday, and make the invoked command a script that bails out immediately if it's not currently in a daylight saving (not "savings") time transition.
This assumes that the system in question is going to be up and running during the transition. If there's a power failure, or if somebody shuts the system down over the weekend, you'll probably need to make arrangements to run the missed job later. (anacron does this, but I haven't used it.)
Daylight saving time transitions occur as time is approaching the reference time.
In March, it ticks 1:59:59 to 3:00. The local clock never actually hits 2:00. So it's not actually possible to schedule for this time. You can schedule for it to run a second early, but not at the actual moment.
Likewise, in November, the clock goes from 1:59:59 to 1:00. By the time 2:00 occurs, the transition has been over for an hour. But if you schedule for 1:59:59, it will run twice.
The above (and your question) assumes North American DST rules. Other time zones transition at different dates and times, or not at all.
See also the dst tag wiki.
In North America, DST changes occur on 2nd Sunday of March, and 1st Sunday of November. The following cron entries would run on these speficic dates:
this happens at 1:59 2nd Sunday of March
59 1 8-14 3 0 echo "One minute before setting DST"
this happens at 1:59 AM 1st Sunday of November
59 1 1-7 11 0 echo "One minute before clearing DST"

How to run cronjob on every month first friday?

I have cronjob to be run on every month first friday evening
i used the below mentioned entry
00 20 1-7 * Fri [ "$(date '+\%a')" = "Fri" ] && $HOME/path/to/my/script.sh > /dev/null 2>&1
This entry should run my script if Friday falls withing 1-7 day of the month, but my script is getting executed even after 7th (i.e on all Fridays of the month).
Please suggest how to fix it.
This is because when you specify a day of month and day of week, cron will execute the job when EITHER of those constraints are true. From the man page for crontab (5):
Note: The day of a command's execution can be specified by two fields —
day of month, and day of week. If both fields are restricted (i.e.,
aren't *), the command will be run when either field matches the cur‐
rent time. For example,
``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st
and 15th of each month, plus every Friday.
There isn't a direct way in cron to do what you want, but cron : how to schedule to run first Sunday of every month describes a workaround by using cron to run your script e.g. every Friday and then calculating in the script if the day of month is in the range 1-7, and only continuing when that is the case.
In response to the comment about using 5 rather than Fri to specify day of week: using Fri is OK, as the man page says:
Months or days of the week can be specified by name.

Is it possible to express 2 week periods as a CRON expression as implemented by Quartz?

Starting from the Jan 1st 2014 the 2 week periods yield the following schedule:
Jan 14
Jan 28
Feb 11
Feb 25
Mar 11
Mar 25
Apr 8
Apr 22
May 6
May 20
etc ...
This is not an idle question. Some companies have exactly 2 week long pay periods, meaning they have 26 pay periods per year rather than 24 for a company that pays twice a month as opposed to every two weeks exactly. I want to know whether I can express the pay schedule of such a company using a CRON expression.
Finally, it is possible that no CRON expression can be used to express what I want, but the Quartz scheduler may have some tricky solution that can ultimately produce the needed schedule. Note, that I can always create a calendar scheduler with 26 explicit triggers - one per pay period. I am not talking about this kind of solution.
I am using the .NET port of the Quartz library.
EDIT
An equally good schedule would be exactly every other Sunday. Note, it is not equivalent to every 2nd and 4th Sunday. Indeed, if a month has 5 Sundays, then the first schedule may result in three instances for that particular month, if started from the first Sunday. Whereas the second schedule always yields exactly two instances per month.
I have a feeling such schedules cannot be expressed in CRON, because they carry over from month to month, whereas CRON resets its month trigger each time a new month starts.
EDIT2
I guess what I am looking for could be expressed by an imaginary DayOfYear CRON field. Helas, no such thing exists. But then again, I could be wrong.
If you're using version 2.0 then you can use the CalendarIntervalTrigger, set the RepeatIntervalUnit to weeks and the RepeatInteval to 2 so that it fires every 2 weeks.

Resources