cron at 15,55 and 35 every other hour - cron

first are these cron anyway similar and if so which ones are redundant?
a) 15,35,55 * * * *
b) 15,35,55 */1 * * *
c) 15,35,55 0/1 * * *
What would be the cron to run at 35 and 15min/55min interchanged by an hour?
e.g
8am - 9am: 8:35am
9am - 10am: 9:15am and 9:55am
10am - 11am: 10:35am
11am - 12am: 11:15am and 11:55am
e.t.c

If you want to know what your cron is doing, then you can easily use crontab-guru to verify.
On your first question, yes the three examples are identical. The manual states the following :
man 5 crontab: Step values can be used in conjunction with ranges. Following a range with /<number> specifies skips of the
number's value through the range. For example, 0-23/2 can be used
in the 'hours' field to specify command execution for every other hour
(the alternative in the V7 standard is
0,2,4,6,8,10,12,14,16,18,20,22). Step values are also permitted
after an asterisk, so if specifying a job to be run every two hours,
you can use */2.
See the following examples:
# Example of job definition:
# .-------------------------- minute (0 - 59)
# | .----------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
15,35,55 * * * * command1
15,35,55 */1 * * * command2
15,35,55 0/1 * * * command3
15,35,55 0-23/1 * * * command4
So in the above example, command[1-4] will be executed at the same time, every hour on minute 15, 35 and 55
For your second question, it is best done this way :
# Example of job definition:
# .------------------------ minute (0 - 59)
# | .----------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr .
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
15 0/2 * * * command1
35,55 1/2 * * * command1
15 8/2 * * * command2
35,55 9/2 * * * command2
15 8-18/2 * * * command3
35,55 9-18/2 * * * command3
Here, command1 will execute on {00,02,04,06,...,22}:15 and {01,03,05,...,23}:{35,15}, command2 will execute on {08,10,...,22}:15 and {09,11,...,23}:{35,15} and command3 will execute on {08,10,...,18}:15 and {09,11,...,17}:{35,15}.

Related

How to set customised schedule in linux cron?

I'm in need to set a cron in crontab like, a php file should be called according to the below schedule
Mon-Fri, 9:00AM to 3:30 PM every one minute
I tried like below,
*/1 * * * * wget http://example.com/trail.php
Can someone help me how to create a cron for the above requirement?
Thanks,
Check the manpage of cron (man 5 crontab). You can do a lot of things, but there's no easy and simple way to achieve what you want. Probably the easiest is to use two entries:
* 9-14 * * * <command>
0-30 15 * * * <command>
You can use following pattern and code
Use the hash sign to prefix a comment
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
#--------------------------------------------------------------------------
*/1 9-14 * * 1,2,3,4,5 wget http://example.com/trail.php
0-30 15 * * 1,2,3,4,5 wget http://example.com/trail.php
Finally, got the obsolete solution.
*/1 9-14 * * 1-5 wget -O /dev/null example/alerts.php
0-30 15 * * 1-5 wget -O /dev/null example/alerts.php
making this work like a charm.
Thanks for the answering, learnt from #Smit Raval and #Ulrich Eckhardt.

Cron scheduling from whole hour to half

I faced with problem. I need to set cron scheduler to start every day every minute from 8 AM to 10:30PM. Who knows, is it possible?
Crontab files give some form of flexibility to define periodic tasks, but in some instances, they are rigid and need some massaging to get the job done. The case presented by the OP is such as case.
Set up a cron-job which runs every minute, every day, but do it only from time tStart up to and including tEnd
Below you find multiple cases which allow you to perform the requested tasks in multiple intervals:
# .----------------------- minute (0 - 59)
# | .--------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
# --------------------------------------------------------------
* 8-21 * * * command1 # daily [08:00, 21:59]
# --------------------------------------------------------------
* 8-21 * * * command2 # daily [08:00, 21:59]
0 22 * * * command2 # daily [22:00, 22:00]
# --------------------------------------------------------------
17-59 8 * * * command3 # daily [08:17, 08:59]
* 9-21 * * * command3 # daily [09:00, 21:59]
0-33 22 * * * command3 # daily [22:00, 22:33]
# --------------------------------------------------------------
* * * * * /path/testcmd 08:30 22:30 && command4
Here, command1 will execute every minute from 08:00 up to and including 21:59. command2 is identical to command1 but the extra line also includes 22:00. command3 has a somewhat more complex form that allows a general time-frame.
While it is possible to define complex cases using multiple lines, it makes maintenance a bit cumbersome. It is than also often useful, in case of complex cases to make use of a conditional command that allows you to perform date-time checks command4 is such a case. This command will only be executed if the command /path/testcmd 08:30 22:30 has a non-zero return-status. The testcmd could be quickly written as:
#!/usr/bin/env bash
tStart="$1"
tEnd="$2"
tNow="$(date "+%H:%M")"
# Lexicographical comparison
[ "$tStart" <= "$tNow" ] && [ "$tNow" <= "$tEnd" ]

How can i run cron-job every 1 hour in Ubuntu 14.04?

I want to run a cron for every 1 hour.
What i tried :
0 */1 * * * /home/username/test.sh
0 * * * * /home/username/test.sh
But, i am not sure, which one is right ?
Can you please help me to decide . which one i should use?
This is the way to set cronjob on ubuntu for every 1 hour
0 * * * * or you can put it in cron.hourly directory
For more details you can refer link:
https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job
# Use the hash sign to prefix a comment
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
#--------------------------------------------------------------------------
* */1 * * * root /bin/sh /home/username/test.sh

Cron Scheduling format guidance

I want to run a cron every 30 seconds between 00:00 and 11:55, every day, every month, any day of the month.
Is the following correct?
0/30 * 0-11 **
The format is as follows:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
So if you want to run every minute between 00.00 and 11.55, every day, every month, any day of the month, you need to combine two different lines:
* 0-10 * * * command
0-55 11 * * * command
Note that to run every 30 seconds you can use the trick described in Running a cron every 30 seconds.
You can try to run your script every 30 seconds using the following:
* 0-11 * * * (sleep 30; /path/to/executable)
So your crontab should looks like
* 0-11 * * * /path/to/executable
0-54 0-11 * * * (sleep 30; /path/to/executable)
Both command lines will be executed at the same time, but the second one will do a 30 seconds sleep before executing your command.
You can try to validate your cron statement with decoder
One of them you can find by the link: http://cronwtf.github.io/

Run cron job every minute during specific hour during 30 minutes

I know I can set up cron for every minute, like
* * * * *
for once a day AFAIK it would be (lets say on 2am)
0 2 * * *
for every 30 minutes
0,30 * * * *
Now, is it possible to run cron job every minute, but during 30 minutes, once a day. For example I need the cron to run every day from 2am to 2:30, and during that time every minute.
thanks
Yes, sure. Use this:
0-30 2 * * *
^^^^ ^
| |
| on hour 2
on minutes from 0 to 30
Remember the format is as follows:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed

Resources