How to set customised schedule in linux cron? - 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.

Related

Bad hour in crontab file

I am trying to run a scheduled job via crontab in linux mint. For that, I am using the crontab -e command to edit a crontab file with the following information:
0 50 * ? * * * sh test.sh
After which I get the error:
"/tmp/crontab.XCXmSA/crontab":22: bad hour
errors in crontab file, can't install.
I tried searching but couldn't find anything that solved the problem. Tried a bunch of different times and still nothing. Any ideas?
You use totally wrong syntax. You add more stars. And questionmark which is not accepted there. Here is the syntax you search:
50 * * * * sh test.sh
And as mentioned in comments you cant have 50 as hour definition
And instead of using explicit shell add it in shebang and make the script executable
You put 50 as an hour. Hour should be in 0..23 range.
# For details see man 4 crontabs
# 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) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

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

How to specify a file to run everyday in crontab

I wants to run a file by everyday using crontab. But i have confusion on specifying the file in crontab. So, please suggest me to mention the file to run everyday ( start of the day, for example: 12:00 am ). Thanks in advance.
If by "file" you mean an executable shell script, then this will do:
00 00 * * * /path/to/the/executable/file.sh
It will run at midnight every day. You should enter this line in a file that will open when you invoke crontab -e command. Once you edit it, save and quit, and crontab will verify whether syntax you entered is correct, so watch the output in the shell.
Use the following format:
0 0 * * * command
check examples here
# minute, hour, day of month, month, day of week
* 12 * * * /home/mydirectory/myscript.sh
* 23 * * * /home/mydirectory/myotherscript.sh
58 23 * * * /home/mydrectory/two_minutes_to_midnight.sh
* 0 * * * /home/mydirectory/goodnight.sh
to make a crontab work, you can read an official doc, there are many of them, and depends on you operational system. But speaking od rules? i'll give some examples:
Common rules :
############################################################################
#
# * * * * * running command
# - - - - -
# | | | | |
# | | | | ----- day of week (0 - 7) (sunday =0 or =7)
# | | | ------- month (1 - 12)
# | | --------- day (1 - 31)
# | ----------- hour (0 - 23)
# ------------- minute (0 - 59)
#
###########################################################################
So if you want to call shell script that works with your files every "x" minutes you can make such a rule :
# work every two minutes
*/2 * * * * /usr/local/sbin/work_script.sh
also, between ranges:
# works every 5 minutes between 7 and 22 hours
*/5 7-22 * * * /usr/local/sbin/work.sh
You can use the below command to intialize the crontab entry to run it everyday.
Syntax : * 10 * * * /home/nagios/myscript.sh
Add the entry #crontab -e and save.
The above command will the command daily at 10 am in the morning.

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/

Set cron job issue

I am first time trying to set cron job in my linux server. I want to set every 10 mins my file will run.
The cron works fine but the problem is that the cron run as following
00:00 00:10 00:20
but i want it in this way
00:05 00:15 00:25
If you have Vixie cron (the most common implementation these days), you can use this syntax:
5-55/10 * * * * command
where 5-55 specifies a range of minutes and /10 says to run once every 10 minutes.
If not, just enumerate all the times you want it to run:
5,15,25,35,45,55 * * * * command
Running man 5 crontab should show you the documentation. (man crontab will show you the document for the crontab command; man 5 crontab describes the file format.)
Reading the manual page you need an entry in your crontab file such as
5,15,25,35,45,55 * * * * <script path>
http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
Check out this link
Easy to remember format:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Resources