Does cron expression in unix/linux allow specifying exact start and end dates - linux

I want to be able to configure something like this.
I want to run job 'X' at 7 AM everyday starting from 29/june/2009 till 30/12/2009. Consider current date as 4/4/2009.

It can be done in a tricky sort of way.
You need three separate cron jobs for that range, all running the same code (X in this case):
one for the 29th and 30th of June ("0 7 29,30 6 * X").
one for every day in the months July through November ("0 7 * 7-11 * X").
one for all but the last day in December ("0 7 1-30 12 * X").
This gives you:
# Min Hr DayOfMonth Month DayOfWeek Command
# --- -- ---------- ----- --------- -------
0 7 29,30 6 * X
0 7 * 7-11 * X
0 7 1-30 12 * X
Then make sure you comment them out before June 29, 2010 comes around. You can add a final cron job on December 31 to email you that it needs to be disabled.
Or you could modify X to exit immediately if the year isn't 2009.
if [[ "$(date +%Y)" != "2009" ]] ; then
exit
fi
Then it won't matter if you forget to disable the jobs.

Yes, mostly. Some cron implementations have support for years, some don't, so we'll assume yours does not. Also, I'm making the assumption that this job is only being run by the cron daemon, so we can use the execute bit to determine whether or not cron should run the job.
Note that you'll need to leave your script as non-executable until such time as you want it to run.
The following cron expressions will do what you want (every day, including weekends). Tweak as you need to:
# Make the job executable on 29 June.
0 6 29 6 * chmod +x /path/to/my/job/script
# Run the job between June and December, only if it's executable.
0 7 * 6-12 * test -x /path/to/my/job/script && /path/to/my/job/script
# Disable execution after 30 December.
0 8 30 12 * chmod -x /path/to/my/job/script

I'm usually a fan of keeping the logic with the program being run. You might think about setting up one cron job that runs the script every day, then have the script decide on its own whether or not it should do anything useful. When the last useful day (Dec 30) has passed, your script could remove itself from the crontab. In the script you can set up the logic with all the comments necessary to describe what you are doing and why.
If your job is a binary program, you might set up a run_script that does this schedule filtering work before calling the program.

You can use this to generate a crontab that runs at specific intervals:
http://www.robertplank.com/cron/
Or this
http://www.webmaster-toolkit.com/cron-generator.shtml
One solution would be to setup 6 crons, 1 for each month, each would run at 7 am every day that month.
It's probably the easiest way, the next one up would be to script it.

No, afaik, you cannot do that.
The cron fields hold the values for minutes, hours, day of month, month and day of week, respectively.
10 5 10 * * means run at 5:10 on every 10th of every month.
10 5 * 12 * means run at 5:10 on every day in december
10 5 * * 1 means run at 5:10 every Monday
You can make it run on a series of specific months, as the crontab format does accept ranges. April through December would be 4-12 in that case for the month field. But that does not take into account your wish for having this limited to 2009.
There is no mechanism to set start and stop dates for cronjob. You can always script this of course. Make a cronjob run every day and check the current date to be before 30/12. If it is 31/12 make it remove itself. Or something more thought through.
A crontab of
0 7 * * 6-12 command_X
would do what you want partially, but it would start at June 1st and run through December 31st. Skipping the first part of June and December 31st would have to be scripted in the X command.

Related

Crontab every monday syntax

I created a cron date but couldn't be sure. I want to run every monday at 16:25. I got help from https://crontab.guru. According to crontab.guru every monday at 16:25 is
25 16 * * MON
Yo can check here https://crontab.guru/#25_16_*_*_MON . I checked the this time on https://www.freeformatter.com/cron-expression-generator-quartz.html but not working. According to freeformatter must be 0 25 16 ? * MON * and next execution dates is correct. Which one is correct. Which one should i use?
I believe different cron implementations have different syntax.
For example, crontab.guru refers to cronitor as mentioned on the website:
crontab guru: The quick and simple editor for cron schedule expressions by Cronitor
Similarly, freeformatter refers to Quartz as mentioned on their website:
Generate a quartz cron expression with an easy to use online interface.
Which one should I use?
So I believe that depends on what cron you're using. For me, using crontab.guru worked fine for scheduling jobs on MacOS.

Using linux' at command - scheduling daily / weekly possible?

I recently came across the at command on the command line, but I am wondering whether I can write a bash script that somehow enables me to tell the at command to run at 8 pm every day, or every monday, every week etc. basically at regular intervals. How is this possible?
at is the wrong tool for this job.
You're looking for cron.
8 pm every day would be :
0 20 * * * /bin/execute/this/script.sh
8 pm every monday would be :
0 20 * * 1 /bin/execute/this/script.sh

Schedule Fortnightly jobs on jenkins

I wanted to schedule fortnightly job on jenkin . It should run every other Monday . I am not able to figure out the cron expression
I did a little research, and it basically comes down to 3 answers that I can find:
Quick answer: You can't.
Complex answer 1: You could manually put in an entry for every other Sunday on a separate line, but this will run into problems when the year changes
0 0 29 4 *
0 0 13 5 *
0 0 27 5 *
0 0 10 6 *
...
Complex answer 2: Create a cron entry that runs every Sunday, and then use something in your build steps that manually checks (toggles) to solve the "every other" part of the problem. (If you need to do the test before the SCM step, the pre-scm-buildstep plugin might help.)
In your project Configure / Build Triggers / Schedule section you can specify #weekly
That will be executed after midnight the first day of the week, based in you JVM locale.
That's the closest you will get from inside Jenkins. Otherwise you'll need to use some external cron job.
0 0 * * #1,#3
According to this, the hash character should allow the above expression to give you a build at midnight, every month, on the first and 3rd Mondays. This is as close as I could find to every-other-Monday.

quartz scheduler crontrigger with minutes "0/15" and hours "11-15" - when does this end?

Given a Quartz Scheduler CronTrigger expression like "0 0/15 11-15 ? * MON-FRI", how is that parsed?
I am correct in assuming the 11-15 does not mean "between 11 and 15" but "when the hour is 11-15, inclusive" - i.e. the expression will trigger every 15 minutes starting at 11:00 and ending at 15:45? Or will it end at 14:45? Or maybe 15:00?
According to Quartz documentation, the "-" modifier is inclusive (for both the start and end values):
"-" used to specify ranges. For example, "10-12" in the hour field means "the hours 10, 11 and 12".
Also, the explanation of this example expression points to the same conclusion:
0 0-5 14 * * ? Fire every minute starting at 2pm and ending at 2:05pm, every day
The hours should match 15 as well, and since 0/15 will still be in effect for the hour 15, the last execution will be 15:45.
#minodudd - Yeah... except it means the "11-15" is inclusive and after that it uses "0/15" to mean every 15 minutes so it runs until 15:45.
I left it running and it did in fact run until 15:45. This seems to be how cron expressions are interpreted in general.

how to write a script in debian , which will kill/stop an application at a certain system time and start it at a certain system time?

i want to write a script in debian , which will make an application stop at a certain system time . Before anyone thinks its a homework , it is not . My internet connection allows unlimited download only in a certain time period , so i would like to stop the torrent application once the time period is over with .
Cron (http://en.wikipedia.org/wiki/Cron) should be your friend here. Add 2 cron jobs to your crontab, one to start the script and one to stop it.
Check into crontab -e - you want something like this in there:
0 2 * * * /path/to/yourtorrentclient start
0 6 * * * /path/to/yourtorrentclient stop
if you want to turn it on at 02:00 every day, and turn it off at 06:00 every day, and assuming yourtorrentclient responds to arguments start and stop. Rewrite as needed.

Resources