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?
asked Feb 12, 2020 at 20:51
3
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
answered Feb 13, 2020 at 7:15
Romeo NinovRomeo Ninov
6,1871 gold badge22 silver badges31 bronze badges
1
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
answered Feb 12, 2020 at 20:58
1
I want my crontab to be every 4 hours but from a certain time 1 pm:
I chose this config:
0 1/4 * * *
but if I save I get the error:
bad hour errors in crontab file, can't install.
This following works perfectly, but then I cannot decide the starting time.
0 */4 * * *
oxr463
1,2201 gold badge9 silver badges19 bronze badges
asked Jul 3, 2019 at 12:17
0
You can’t have 1/4
as the hours. This means «run at 01:00 (1am), every 4 hours«. What you need is «run from 01:00 (1am) until the end of the day, every 4 hours«.
0 1-23/4 * * *
You could also write this with the explicit hour numbers listed out, but I personally find this harder to process visually, and it’s not so obvious that it means «every four hours from 1am«:
0 1,5,9,13,17,21 * * *
answered Jul 3, 2019 at 12:39
roaimaroaima
99.1k14 gold badges126 silver badges239 bronze badges
3
Depending on your flavor of cron, it may not love the / notation.
Rather than trying to make it super-compact, just tell it what you want it to do in a more acceptable to most/all versions of cron fashion. Might have the advantage of being more obvious to more humans, too. It’s a simple system — you really shouldn’t need a «tool» to create a crontab beyond a text editor. Since noon happens after 9, you only need to provide a «special case» for 9, 10 and 11 on Sunday and Saturday.
# every day at 2 minutes past the hour from Noon to 11 PM
2 12-23 * * * tills13 python3 /home/tills13/script.py --sync
# weekends at 2 past the hours of 9, 10 and 11
2 9-11 * * 0,6 tills13 python3 /home/tills13/script.py --sync
Should also work for weekend (Sunday is 0 and 7):
2 9-11 * * 6-7 tills13 python3 /home/tills13/script.py --sync
if you want every hour at 2 minutes past.
I haven’t bothered to decode the / notation fully, (don’t recall it from my cron days 30 years ago and the wikipedia article is not comprehensive WRT it) but if you actually want every 2 minutes, just list them — 2,4,6,8,10,12 (etc)
9-11 in the hour field is equivalent to 9,10,11 (for example) and can be written either way and work. If you’d prefer to run ON the hour, change 2 in the minutes field to 0.
Minute Hour Day-of-Month Month Day-of-Week Command
is all there is to standard crontab notation, with ranges of 0-59, 0-23, 1-31, 1-12, and 0-6 (but 7 is commonly accepted as 0 here)
— the time fields can have comma separated lists or dash-separated ranges, or the * means all — whitespace separates the fields.
In this article, I will explain about the «errors in crontab file, can’t install» error which I was getting while setting up the Crontab. This error is very common and can come up due to various reasons hence it is important to understand the crontab parameters. I will go through multiple scenarios in which this error could occur and how can we resolve this. But before that it is important to understand the crontab parameters through below example.
* * * * * /home/centos/example.sh
| | | | | |
| | | | | Command or Script to execute
| | | | |
| | | | Day of week(0-6)
| | | |
| | | Month(1-12)
| | |
| | Day of Month(1-31)
| |
| Hour(0-23)
|
Min(0-59)
Also Read: 11 Unix/Linux chmod Command examples to Change File Permissions
Scenario 1: When number of crontab field is more than expected
I have been trying to set crontab from User root by running crontab -e
command. After setting up the required fields I tried to save the crontab file multiple times but every time I got below error and it was not allowing me to save the file.
[root@localhost ~]# crontab -e no crontab for root - using an empty one crontab: installing new crontab "/tmp/crontab.gsQNhu":1: bad command errors in crontab file, can't install. Do you want to retry the same edit? y crontab: installing new crontab "/tmp/crontab.gsQNhu":1: bad command errors in crontab file, can't install. Do you want to retry the same edit? y
-e : Edits the current crontab using the editor specified by the VISUAL or EDITOR environment variables. More can be checked on crontab Man Page.
Below is what my crontab file looks like when i got the above error. If you see the below output carefully then you will see an extra star(*)
field due to which above error was showing.
0 3 * * * * /home/centos/example.sh
To resolve this issue i have removed that extra star(*)
mark and then saved the file again. Then i am able to see below output which shows that crontab is successfully saved.
crontab: installing new crontab
Scenario 2: If you forget to mention the Script in Crontab File
Another scenario could be when you forget to mention the Script in Crontab File like the one showing below. Sometimes you might forget to mention the Script file which needs to run as part of cron job and then try to save the file. In those cases also you will get the same "errors in crontab file, can't install"
error.
0 2 * * *
To fix this error you need to edit the crontab file and mention the script file name that needs to be run as part of the cron job.
Scenario 3: You might get «errors in crontab file can’t install bad minute»
I have also seen a scenario where sometimes we might add an extra line like the one mentioned below then if you try to save this crontab file you will get "errors in crontab file can't install bad minute"
error. This is because crontab expects to have a minute value mentioned at the start of every line. In this example, we have used a script called backup.sh
at the start of the Line hence it will throw bad minute error.
0 3 * * * /home/centos/example.sh backup.sh
Scenario 4: You might get «errors in crontab file can’t install bad day-of-week»
I have also seen a scenario where sometimes Linux professionals uses some value in week section which is not expected hence started getting "errors in crontab file can't install bad day-of-week"
error when try to save the file. One of the such instance I have shown below where week section expecting a value between 0 and 6 but instead 9 is mentioned which results in "bad day of the week"
error.
* 4 0 * 9 /home/centos/example.sh
Scenario 5: You might get «errors in crontab file can’t install bad command»
There is another common mistake I have seen Linux Professionals or Beginners often doing is that they will some unexpected command or a command which is not set in $PATH
which results into "errors in crontab file can't install bad command"
error. One such instance I have shown here where jq
command is not there in any of the Linux binary path hence it is not detecting this command. In these cases either you need to provide the complete path of the command or add the command path in $PATH
so that system detects that command.
0/10 * * * * jq
Scenario 6: You might get «bad day-of-month errors in crontab file can’t install»
Finally I have also seen Linux professionals or beginners doing a mistake assuming month section as days of month section and will use any value greater that 12 resulting in "bad day-of-month errors in crontab file can't install"
error. Here you need to be very careful in understanding the difference between days of the month and month of the year. Month of the year accepts value between 1 to 12 whereas days of the month accepts values between 1 to 31.
* * * 15 * /home/centos/example.sh
Popular Recommendations:-
Learn HTML Image Maps(v5) with Best Examples
Easy Steps to Install GCC(C and C++ Compiler) on CentOS 7
C# data types with Best Examples (.NET v4.7)
How to Transfer Files to an AWS EC2 Instance Using WinSCP in 3 Easy Steps
Learn HTML Tables(v5) with Best Examples
How to Install PHP on RedHat/CentOS 7 with Easy Steps
How to Install Ruby on Ubuntu 18.04 with Easy Steps
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Modify hour crontab UNIX
Hello,
I wish to modify the day of launch of a «batch» in the crontab on several servers with awk or sed.
Can you help me ?
Beforehand thank you
00 18 * * 1 /box/Pra/bin/systeme_reverse.ksh -s >/dev/null 2>&1 (7 Replies)
Discussion started by: khalidou13
2. Shell Programming and Scripting
E-mail based on crontab hour
I have a cron: 0 5,11,17,23 * * * /home/oracle/scripts/sysize.ksh
This cron will trigger
cat dbsz.txt | mail -s «$TODAY: PROD DB Size» $RECIPIENTS
I don’t want to get the e-mail notice 4 times a day. Can I have just one e-mail triggered at 11 AM?
Please advise.
Thank you (3 Replies)
Discussion started by: Daniel Gate
3. Solaris
Solaris 9 Zone : Date command in crontab shows delayed(One Hour) output
SOLARIS 9 Zone :
date command in crontab shows delayed(One Hour) output
Hi folks,
the date command shows the correct date and time, How ever, if the date command executed through crontab in any form of scrip the output shows as one hour delayed, similar to date -u..
Can some one help in… (12 Replies)
Discussion started by: judi
4. UNIX for Dummies Questions & Answers
Crontab not working with the hour configuration
Hi,
When I set the crontab to run every minute, every hour, it works fine.
* * * * * env > /tmp/env.output
However I want to run it every day at 8:00 AM and it does not run.
* 8 * * * env > /tmp/env.output
I ran the ‘date’ command which says it’s 8AM PST and also the ‘TZ’… (5 Replies)
Discussion started by: samantha13
5. AIX
crontab 1 hour off from current time
This is a new one on me. We upgraded a system from AIX 5.3 TL 7 to 6.1 TL 7 yesterday. The app people notified us that their cron jobs weren’t running at the right time. So I made a test cron entry and here’s what I’ve found:
# crontab -l
* * * * * /usr/bin/date > /tmp/test.log 2>&1
# cat… (2 Replies)
Discussion started by: homeyjoe
6. Shell Programming and Scripting
To Run a job every hour without using crontab
Hi,
Can anyone help me in finding how to run a specific job every hour without using crontab.
Say for example i need to run a command ps -aux in the starting of every hour.. (3 Replies)
Discussion started by: glv
7. UNIX for Dummies Questions & Answers
Cannot run crontab :1: bad minute error
Hi can any one help me out.
while running crontab , m getting error bad minute time..
how to resolve this error.
i created 1 txt file a.cron
mirrordir -vm homet homet1
& i saved it.
then i created a crontab file ..with
crontab -e
& i added a line in it.
* 1 * * * roota.cron
& i save… (2 Replies)
Discussion started by: unxdost114
8. Shell Programming and Scripting
Sed or Awk for modify hour in a crontab AIX
Hi,
I want to modifiy the hour in the crontab AIX 5.3 for this line:
Input:
00 22 * * * /outillage/script_exploit/bin/SavOffline.ksh > /dev/null 2>&1
Output:
30 20 * * * /outillage/script_exploit/bin/SavOffline.ksh > /dev/null 2>&1
With the awk or sed function through a ssh -q… (1 Reply)
Discussion started by: khalidou13
10. UNIX for Advanced & Expert Users
crontab : bad minute error
when i say
$crontab z
it says ==>
«z»:6: bad minute
«z»:6: bad minute
errors in crontab file, can’t install.
any clue why its happening? (1 Reply)
Discussion started by: crackthehit007
This is pretty clearly a bug, and it seems to affect Red Hat systems of a number of flavours. I’ve confirmed the behaviour you describe on CentOS 5, C6, Fedora 19, and Fedora 20.
For crontab
entries of the form
30 a-b/c * * * /bin/ls
it seems that if there exists no positive integer n for which a+nc
is in the range [24,b]
, the invalidity check for the final hour (b
) is ineffective. That is, you can write gibberish as long as the OS doesn’t actually have to do anything about it; if you specify a range and spacing such that it would actually trigger a job at an invalid hour (which includes 24
) then crontab
complains.
I’ve just persuaded my F20 system to accept the crontab
entry
30 6-33/16 * * * /bin/ls
which is insane by any standards. Similarly, 30 2-33/14 * * * /bin/ls
and 30 1-33/16 * * * /bin/ls
both give the (desirable) bad hour
error, while 30 2-27/14 * * * /bin/ls
is valid.
Congratulations, Arun, I think you’ve found a bona fide bug in Red Hat’s crontab
(it isn’t present on Ubuntu 12.04LTS, though that’s the only other system I have to hand for testing). Do you want to log it with with RH bugzilla, or shall I?
This is a common error
Context
You will encounter this error when you attempt to create or update
a crontab file that contains a syntax error.
What causes this error
Crontab files, while very simple, have a precise specification. Among other things, a crontab must:
- Start with a valid cron schedule, environment variable or comment on each line
- Contain an executable expression after the schedule
- End with a newline
- Include a username after the schedule for system-wide crontab files
When any syntax error occurs, cron will return the message errors in crontab file can't install
.
Fixing Errors in crontab file can’t install
This error message often includes a more specific problem that you can address, for example:
- Bad Minute Crontab Error
- Missing Newline Before EOF Error
If you are still stuck, review your local documentation for further guidance on crontab format. You can do this from your command line by running man 5 crontab
Related Guides
Cronitor automatically finds and monitors cron jobs. Complete your setup in minutes.
Get Started Free
-
#1
Migration from Plesk 10.4 to 11.5 failing:
Issue
Within Plesk 10.4 server the user has no existing scheduled jobs (Cronjobs)
When migrating a domain to 11.5 the following error is generated:
Internal server error: /usr/bin/crontab execution failed:
«-«:1: bad hour
errors in crontab file, can’t install.
System error: crontab execution error
Checking the old server:
#cat /var/spool/cron/crontabs/username
Code:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (crontabMzaOJb installed on Tue Dec 3 09:59:20 2013)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
[email protected]
and
#crontab -e -u username
This looks fine
Technical Stuff
Some of the log entries:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<execution-result-mixed status="warnings" log-location="">
<backup status="success" log-location="/var/cache/20131204143728928/migration.result">
</backup>
<transfer status="success">
</transfer>
<restore status="warnings">
<conflict-resolve status="success" log-location="">
</conflict-resolve>
<deploy status="warnings" log-location="">
<object type="domain" name="example.com">
<message code="ExecCliGate::InternalServerError" severity="warning" id="cdf616a6-74d2-401b-80c9-fd1fb4743fea">
<description>Internal server error: <cli><failure>/usr/bin/crontab execution failed:
&quot;-&quot;:1: bad hour
errors in crontab file, can't install.
System error: crontab execution error
</failure></cli></description>
</message>
</object>
</deploy>
</restore>
</execution-result-mixed>
And the dump_object1.xml file shows:
Code:
<cid-rsync unpacksize="4616548" path="/var/www/vhosts/example.com/statistics/ftpstat/" type="ftp_stat"/><cid-rsync unpacksize="681566" path="/var/www/vhosts/example.com/statistics/anon_ftpstat/" type="anon_ftpstat"/><cid-rsync unpacksize="4096" path="/var/www/vhosts/example.com/pd/" type="pd"/><cid-rsync unpacksize="173306461" path="/var/www/vhosts/example.com/statistics/logs/" type="logs"/></content><preferences><sysuser name="username"><password type="sym">$AES-128-CBC$3ks+yvMtFusCasc424gfwrg24gh3rgv==kn24p8kPFoQ==</password><cron>[email protected]
</cron></sysuser><logrotation compress="true" max-number-of-logfiles="10" enabled="true"><logrotation-maxsize>204800000</logrotation-maxsize></logrotation><webalizer/><perfomance><max-connections>-1</max-connections><traffic-bandwidth>
This clearly shows a newline after the email address and is probably the cause of the issue.
Solution
Empty the crontab file for the user, this can be done via a text editor via SSH:
#crontab -e -u username
Run the migration again and it should now work fine.
-
#2
Had the same issue on a different server, this time migrating from Plesk 9.5 to 11.5
logs/migration-2013-12-05-08-12-38-668/deployer.log:<subscription-info><sysuser name=»username»><cron>[email protected]
logs/migration-2013-12-05-08-12-38-668/deployer.log:*/5 * * * * php -q /var/www/vhosts/example.co.uk/httpdocs/cron.php >/dev/null 2>&1
logs/migration-2013-12-05-08-12-38-668/deployer.log:</cron></sysuser></subscription-info>
logs/migration-2013-12-05-08-12-38-668/deployer.log:[2013-12-05 08:33:28.734| 5671] INFO: HTTP response: <cli><failure>/usr/bin/crontab execution failed:
logs/migration-2013-12-05-08-12-38-668/deployer.log:errors in crontab file, can’t install.
logs/migration-2013-12-05-08-12-38-668/deployer.log:System error: crontab execution error
logs/migration-2013-12-05-08-12-38-668/deployer.log:[2013-12-05 08:33:28.734| 5671] INFO: ExecCliGate::InternalServerError[decb252b-032f-40b6-8ecb-286c0b646dcd]: Internal server error: <cli><failure>/usr/bin/crontab execution failed:
logs/migration-2013-12-05-08-12-38-668/deployer.log:errors in crontab file, can’t install.
logs/migration-2013-12-05-08-12-38-668/deployer.log:System error: crontab execution error
The above was a grep for «cron» on the log files, so may be missing some information.
The crontab for this user contained:
Code:
[email protected]
PATH=$PATH:/usr/bin:/usr/local/bin
*/5 * * * * php -q /var/www/vhosts/example.co.uk/httpdocs/cron.php >/dev/null 2>&1
I systematically deleted each line from the top and tried the migration, but it failed every time. Once the cronjob was removed completely the migration completed successfully.
I created 3 daily cron jobs to run.
Below are the three that are placed in etc/cron.daily
rkhunter.sh
#!/bin/sh
(
rkhunter --versioncheck
rkhunter --update
rkhunter --cronjob --report-warnings-only
) | mail -s 'rkhunter Daily Run (my server)' me@email.com
chkrootkit.sh
#!/bin/bash
chkrootkit | mail -s "chkrootkit Daily Run (my server)" me@email.com
logwatch.sh
#!/bin/sh
(
logwatch
) | mail -s 'logwatch Daily Log (my server)' me@email.com
I replaced me@email.com ofcourse with my email.
If I run this cronjob manually it works fine ./nameoffile.sh
But it doesn’t run daily, what can be the cause or how can I check into this?
Seth
56.2k43 gold badges144 silver badges196 bronze badges
asked Aug 25, 2013 at 20:55
2
According to this response, the problem lies with the .sh extension. Remove that (so for example rename your file from rkhunter.sh to rkhunter.
To confirm run the following command run-parts --test /etc/cron.daily
If your script (rkhunter) is included in the results, all is good. For more information on the run-parts command, read the man pages on it man run-parts
answered Feb 4, 2014 at 21:04
grandmaestrgrandmaestr
1,0717 silver badges4 bronze badges
2
In my system it was because anacron wasn’t installed.
grep run-parts /etc/crontab
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
So either install anacron or remove the test -x /usr/sbin/anacron
answered Sep 15, 2014 at 11:57
NatimNatim
88311 silver badges23 bronze badges
6
I think files with extensions are ignored.
run:
run-parts --test /etc/cron.daily
If you don’t see your scripts listed, remove the .sh extensions and try again.
answered Mar 20, 2014 at 23:21
rharrisorharriso
2412 silver badges3 bronze badges
Adding to Stef answer, you also should make sure that they have the executable bit:
$ ls -l
-rwxr-xr-x 1 root root 268 Jun 1 08:06 00logwatch
-rwxr-xr-x 1 root root 311 May 22 2012 0anacron
-rwxr-xr-x 1 root root 15007 Jun 6 14:08 apt
You should be able to run them using chmod +x filename
.
answered Aug 25, 2013 at 23:53
BraiamBraiam
66.2k30 gold badges174 silver badges262 bronze badges
1
There are two possible suspects that usually cause cron
jobs not being able to run.
The first is permissions problems, that is a user can run the script/command but the cron daemon cannot because the job is in the wrong user’s cron jobs. For example the user creates a script or runs a command with elevated privileges i.e using sudo
, then adds the tested script/command to his list of cron jobs (crontab
). The result is that the user’s cron job will not be able to run since it needs elevated privileges.
- To put a cron job in current user’s crontab type
crontab -e
- To put a cron job in root’s crontab type
sudo crontab -e
The second reason is the paths, in order to be sure that the script will execute, the user must add the full path to the script to be executed in crontab. Another solution would be to expand the root users PATH variable by putting the following line at the top of their crontab file:
PATH=/usr/sbin:/usr/bin:/sbin:/bin
as the community wiki mentions.
You may want to read the community wiki about cron as it provides further details about the above.
jjmerelo
3312 silver badges12 bronze badges
answered Aug 25, 2013 at 23:00
Stef KStef K
4,7462 gold badges24 silver badges36 bronze badges
11
Rename your file to not have the .sh extension
To verify this is the issue, try
sudo run-parts --list /etc/cron.daily
you will see it’s not listed. So run:
mv script.sh script
and try listing again. It should be listed.
anonymous2
4,2286 gold badges31 silver badges61 bronze badges
answered Apr 4, 2017 at 13:55
1
The default time to execute daily scripts is 06:25 AM. If your machine is off at this time (every day), they don’t run.
anacron runs scripts that were missed because the machine was off.
You can configure a different time by editing /etc/crontab
.
answered Mar 1, 2020 at 8:52
orgadsorgads
1711 silver badge3 bronze badges
I could not get it run with anacron, I removed anacron from /etc/crontab
and executed apt remove --purge anacron
and it works right away.
I do not understand why we need two scheduler.
answered Jul 9, 2018 at 8:28
Same situation today here
I did
sudo journalctl -u cron -b | grep -i error
and found
cron[815]: Error: bad hour; while reading /etc/crontab
cron[815]: (*system*) ERROR (Syntax error, this crontab file will be ignored)
I discovered that someone (me !!!!) added a line starting with
20 38 ...
and obviously the 38th hour doesn’t exist !
answered Dec 20, 2019 at 15:06
realteborealtebo
4179 silver badges23 bronze badges
Similar issue solved in these steps
-
Restart cron service to see what happened in logs.
service cron restart
-
Monitor logs
grep -i cron /var/log/syslog
show
cron[37426]: (**) ERROR (Missing newline before EOF, this crontab file will be ignored)
cron[37426]: (CRON) INFO (Skipping @reboot jobs — not system startup)
Logs show the misbehavior relates to my cron EOF.
After cat it, cat /etc/cron.d/mycron
prove that this file doesn’t have newline at last line.
- Solution is to append newline to last line.
echo >> /etc/cron.d/mycron
If open the file with Vim, then save (:wq
) its also append newline to last line of file.
force vim not append newline at end of file
answered Feb 1, 2021 at 11:15
EsmaeelEEsmaeelE
3195 silver badges15 bronze badges