Crontab error: "/tmp/crontab.calJpk":5: bad day-of-month - linux

I am trying to run a crontab with the expression given below. But i am getting bad day-of-month error.
My requirement is to run this code:
everyday except Sunday
every hour starting 2 am till 10pm
0 2-22 ? * 0-6 * /usr/bin/python /my/location/to/python_code_for_cron/sampletest.py
Is there some issue with the cron expression or something else that i need to install?
FYI: I am using crontab -e to edit my crontab

You just have too many arguments in there. Read man -s5 crontab for more info. The fields are:
minute hour day-of-month month day-of-week
Also, 0-6 is the same as * for day-of-week. Your line should read:
0 2-22 * * 1-6 /usr/bin/python /my/location/to/python_code_for_cron/sampletest.py

Related

Set a cron job to a specific timezone on Centos 7

I'm trying to set the cron jobs to run on a specific timezone, but I think I'm missing something.
I've tried to add TZ (And CRON_TZ) on top of the jobs in the /var/spool/cron/ file, but it doesn't seem to work.
TZ=Europe/Rome
* * * * * /usr/local/bin/php /home/user/folder/file.php
I've installed CWP7pro
from the manual:
Every * means something:
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)
if you want every minute use this
*/1
example:
*/15 * * 1 * means every 15 minutes on the 1st day of the month
Edit /etc/crontab or use
crontab -e (edit)
CRON_TZ should be used instead of TZ or use TZ inline
*/15 * * 1 * TZ=Europe/Rome echo "do something"

Running a cron job at 2:30 AM everyday

How to configure a cron job to run every night at 2:30? I know how to make it run at 2, but not 2:30.
crontab -e
add:
30 2 * * * /your/command
To edit:
crontab -e
Add this command line:
30 2 * * * /your/command
Crontab Format:
MIN HOUR DOM MON DOW CMD
Format Meanings and Allowed Value:
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
CMD Command Any command to be executed.
Restart cron with latest data:
service crond restart
As seen in the other answers, the syntax to use is:
30 2 * * * /your/command
# ^ ^
# | hour
# minute
Following the crontab standard format:
+---------------- 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
It is also useful to use crontab.guru to check crontab expressions.
The expressions are added into crontab using crontab -e. Once you are done, save and exit (if you are using vi, typing :x does it). The good think of using this tool is that if you write an invalid command you are likely to get a message prompt on the form:
$ crontab -e
crontab: installing new crontab
"/tmp/crontab.tNt1NL/crontab":7: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? (y/n)
If you have further problems with crontab not running you can check Debugging crontab or Why is crontab not executing my PHP script?.
An easy way to write cron is to use the online cron generator
It will generate the line for you. One thing to note is that if you wish to run it each day (not just weekdays) you need to highlight all the days.
As an addition to the all above mentioned great answers, check the https://crontab.guru/ - a useful online resource for checking your crontab syntax.
What you get is human readable representation of what you have specified.
See the examples below:
30 2 * * * (answer of this question)
#daily
59 23 31 12 *
30 2 * * * wget https://www.yoursite.com/your_function_name
The first part is for setting cron job and the next part to call your function.
30 2 * * * Every Day at 2:30 Am
30-31 2 * * * Every Day at 2:30 -31 am
Along with he answers its important to understand the cron expressions , i face a lot of difficulty in understanding .
But an intuitive way to understand is given here .

setup cron tab to specific time of during weekdays

I am trying to setup a cron job on a Ubuntu server. We want the cron job to run the script at certain times of the day and on some specific days of the week. For example, we want to setup a cron job that runs the script with the following sequence:
Execute the script every 2 minutes from 9 am to 2 pm during the weekdays.
This is what I have been able to do so far:
*/2 09-14 * * * /path_to_script
What should I do for the weekdays?
Same as you did for hours:
*/2 09-18 * * 1-5 /path_to_script
0 and 7 stand for Sunday
6 stands for Saturday
so, 1-5 means from Monday to Friday
You state 2pm in your requirement, hour range should end at 14 instead of 18 (which is 6pm).
*/2 9-14 * * 1-5 /path_to_script
man crontab
http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5
In fact the last hour you want the script to run is 13:00 to 13:59, so you want:
*/2 9-13 * * 1-5 /path_to_script
meaning the first runtime will be 9:00, then 9:02, and so on until 13:58 which will be the last run as 14:00 is not included.

Running a cron job 3 times (1 pm, 2 pm and 3 pm for example)?

I am not sure how to run a cron job at 3 specific hours every day. I want to run it at 1pm, 2 pm and 3pm.
Is it possible, using a single expression?
you may use this:
# m h dom mon dow command
0 13,14,15 * * * /home/user/command
your /home/user/command will be run at 13:00, 14:00 and 15:00
As lenik stated, it can be done in single expression.
0 13,14,15 * * * <your-script-to-run>
Check this geedkstuff link for more examples
While the given answers are correct, an unexperienced user might not know where to put this expression. You have to edit the crontab file, like:
crontab -e
There you add
0 13,14,15 * * * /home/user/command
to execute your command at 13:00, 14:00 and 15:00. Also note that user has to be substituted with the user account the command is executed in.
You can try the following as well:
0 13-15 * * * /home/apps/sample.sh
To anyone landing here --> useful tool:
https://crontab.guru/
Please prefer range+step over commas:
Example: Run every 2h from 9h to 16h
m h dom mon dow command
0 9-16/2 * * * /home/user/command
Also applicable to minutes:
m h dom mon dow command
10-30/10 9-16/2 * * * /home/user/command
Crontab guru shows what it means, and the next scheduled jobs.
For example I typed this cron at 10h05:

Scheduling shell script in crontab

I have a shell script that I can run with root user this way:
root#vivid-15:~# ./backup.sh
It's on /root/backup.sh. Now, how do I schedule it on crontab to execute every day at 01:00 AM? I done this:
0 1 * * * root
But now I don't know how to proceed the command to do that.
Have you tried this? Also, a "1" in the hour field means 1am, not 1pm.
0 1 * * * root /root/backup.sh
Edit: changed the 13 (1pm) back to 1 (1am).
Crontab format:
MIN HOUR DAY MON WEEKDAY CMD
I don't know that you need to define what user you want it to run as when its in crontab -- commands will be run as the user who makes the entries with crontab -e. To create a cron process that runs as root, either login as root or set it up with $ sudo crontab -e
I think you're looking for something more like this:
0 1 * * * /root/backup.sh

Resources