Bad hour in crontab file - cron

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

Related

Cron script doesn't work unless called by sh command

I have a script saved in file named file.sh
It is executed by command
chmod +x file.sh
file.sh
#!/bin/sh
find /var/log/*.gz -printf "%T# %Tc %p\n"|sort -n|tail -n +6|sudo xargs rm|echo"test"|wall
I am trying to execute it by cron job.
Crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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
* * * * * root /etc/file.sh
Script is launched every minute by cron job, I am getting echo in terminal.
But xargs rm doest works . I do know why.
When I execute command sudo sh file.sh, is OK, and file is deleted.
Please help me find the reason why the script is not working in cron-job.
It's strange.
Thank You !
The problem is your printf statement ... I'm fairly certain that you should also see error messages from rm in root's mail to the effect that e.g.:
1634986839.0000000000 Sun 24 Oct 2021 12:00:39 AM UTC /var/log/syslog.2.gz doesn't exist.

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.

CronTab - Not Working

I seem to be having problems with crontab, the following works correctly
* * * * * TERM="xterm";/usr/bin/lynx -dump http://myurl.com
However, when I try scheduling one in for every Sunday at 20:40
*/40 */20 * * 0 TERM="xterm";/usr/bin/lynx -dump http://myurl.com
It doesn't work, is there something wrong with my syntax? I actually used a generator to write this.
I'm running CentOS, the cronjob should visit a PHP page that then uploads to DropBox - visiting the URL via the browser works. I'm validating my results by checking the dropbox uploaded timestamp on the file.
UPDATE - For some reason, using CURL instead worked fine :)
Here's a cron syntax checker
It said
The command TERM="xterm";/usr/bin/lynx -dump http://myurl.com will execute every 40 minutes of every 20 hours on of every month.
Which isn't what you're wanting I think.
The slashes you are using make increments of ranges. See cron special characters
I recommend you read up on the cron syntax. You want:
40 20 * * 0
Explanation:
* * * * * command to be executed
^ ^ ^ ^ ^
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

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)

Cron not working as expected

I have an interesting problem with a cron job. I'm on a server with Cent OS and we're using Vixie cron. All this is very straighforward and i have a backup job that i want to run once every day at 01.00, so i created this simple crontab entry:
* 1 * * * /path/to/my/simpleJob.sh
What happens is this: at 01.00.02, the job runs as expected. Then cron (or something) continues to run the job every minute. Can anyone out there help me out? I have no idea what might be the root cause for this.
/M
The format is:
+---------------- 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 in your case being
* 1 * * * /path/to/my/simpleJob.sh
it means at hour 1, any minute.
To have it working just at 1.00, change it to:
0 1 * * * /path/to/my/simpleJob.sh
The CronSandbox at Dataphyx lets you play about with the date/time values - you see a schedule of runtimes for whatever combination you put in.

Resources