31

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?

2
  • 3
    Make sure that the files you created in cron.daily/weekly/hourly/etc are executable just do a chmod +x /etc/cron.daily/whatever Sep 9 '16 at 15:32
  • 1
    IMPORTANT: the Debian-specific changes to cron state that files you place in the cron.daily, etc. folders can only use alphanumeric characters. If you use a dot (.) in the file name, it will specifically exclude it from running. Others (below) have stated this, but it's helpful to state it up here where it's possible to be seen.
    – WineSoaked
    May 11 '20 at 20:57

10 Answers 10

91

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

2
  • 1
    This is the answer I was looking for, after various test, I realized that another script file without sh extension is executed Jun 10 '16 at 19:55
  • 12
    as @rharriso said in his answer. it's not so much an issue with ".sh" as an issue with ".". any file with any extension will be missed. to quote directly from man run-parts "names must consist entirely of ASCII upper- and lower-case letters, ASCII digits, ASCII underscores, and ASCII minus-hyphens" Jun 12 '17 at 12:23
15

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

6
  • 1
    +1 Isn't anacron installed by default? I would have expected that. I think that would solve it for me. Thanks.
    – lepe
    Mar 14 '15 at 3:15
  • Sure enough, it wasn't there on mine.. FFS, I am sure it WAS, as the script was executing a few months ago!: dpkg --get-selections | grep cron .. <swears>
    – Grizly
    May 19 '15 at 22:54
  • Yes I don't know what happened either since it is a package usually installed at startup.
    – Natim
    May 20 '15 at 8:10
  • 11
    This is not really correct. anacron isn't necessary; the || operator in the crontab commands executes run-parts when anacron is NOT installed. When anacron is installed, it makes those daily/weekly/monthly run-parts commands redundant.
    – TalkLittle
    Feb 23 '17 at 22:47
  • So maybe it was because run-parts didn't work? In anycase installing anacron fixed it for me.
    – Natim
    Nov 29 '19 at 13:42
11

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.

8

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.

1
  • Which files are these? is it the content of /etc/logrotate.d folder ?
    – realtebo
    Dec 20 '19 at 14:44
6

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.

11
  • So do i just put the filename in there?
    – sonicboom
    Aug 26 '13 at 21:17
  • It is actually saying that there is no previous cron job for root and you are going to write your first and then it asks you to pick an editor in order to modify the crontab. Just pick one from the menu (1.bin/ed, etc). Pick nano its easy, just pay attention to the instructions.
    – Stef K
    Aug 26 '13 at 21:22
  • So to run once a day at 10PM i would put * 22 * * * test > rkhunter.sh right?
    – sonicboom
    Aug 26 '13 at 21:27
  • ah awesome! ill try it right now!
    – sonicboom
    Aug 26 '13 at 22:03
  • What is the test>rkhunter.sh for?
    – sonicboom
    Aug 26 '13 at 22:04
4

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.

1
  • This problems seems to effect any executable that has an extension. I had a file names "filename.ca" and it also would not list it until I renamed it too "filename" Nov 28 '18 at 0: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.

0

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.

0

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 !

0

Similar issue solved in these steps

  1. Restart cron service to see what happened in logs. service cron restart

  2. 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.

  1. 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.