36

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
  • 58,122
sonicboom
  • 1,153
  • 2
  • 9
  • 13
  • 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 – Turgut Kalfaoglu Sep 09 '16 at 15:32
  • 2
    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
  • Check whether their are entries in the journal, run: * journalctl --unit=crond That will tell if it was ran and when was it ran. – Jose Sanchez May 27 '23 at 15:33

11 Answers11

105

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

grandmaestr
  • 1,161
  • 1
    This is the answer I was looking for, after various test, I realized that another script file without sh extension is executed – Albert Català Jun 10 '16 at 19:55
  • 16
    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" – northern-bradley Jun 12 '17 at 12:23
18

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

Natim
  • 909
  • 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 03: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 .. – 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 08: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
  • See https://askubuntu.com/a/437114/11333 – Natim Nov 29 '19 at 13:42
17

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.

rharriso
  • 271
  • 2
  • 3
9

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.

Braiam
  • 67,791
  • 32
  • 179
  • 269
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.

jjmerelo
  • 331
Stef K
  • 4,836
6

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,298
  • 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" – kiwicomb123 Nov 28 '18 at 00: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.

orgads
  • 201
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 !

realtebo
  • 399
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

EsmaeelE
  • 337
0

I ran into this issue too. When I ran run-parts --test /etc/cron.daily my script showed up in the list, but actually triggering the scripts using run-parts /etc/cron.daily brought up an error with my script:

run-parts: failed to exec /etc/cron.daily/docker-prune: Exec format error
run-parts: /etc/cron.daily/docker-prune exited with return code 1

Turns out I'd forgotten to include the shebang (#!/bin/bash or whathaveyou) at the top of my script.

Don't just test if your script is included in the list of things to be run - actually run it to make sure it works!

gregdev
  • 101