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)' chkrootkit.sh
#!/bin/bash
chkrootkit | mail -s "chkrootkit Daily Run (my server)" logwatch.sh
#!/bin/sh
(
logwatch
) | mail -s 'logwatch Daily Log (my server)' I replaced 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?
210 Answers
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
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
6I 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.
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 aptYou should be able to run them using chmod +x filename.
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:/binas the community wiki mentions.
You may want to read the community wiki about cron as it provides further details about the above.
11Rename 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 scriptand try listing again. It should be listed.
1The 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.
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.
Same situation today here
I did
sudo journalctl -u cron -b | grep -i errorand 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 !
Similar issue solved in these steps
Restart cron service to see what happened in logs.
service cron restartMonitor logs
grep -i cron /var/log/syslogshow
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.