19

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
    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 9 '16 at 15:32
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.

  • 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
68

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

  • 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
  • 4
    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
10

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

  • +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
  • 6
    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
8

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.

4

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.

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.

  • 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 0:55
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.

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.