cron.daily jobs not running

60,764

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

Solution 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

Solution 3

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

Solution 4

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.

Solution 5

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.

Share:
60,764

Related videos on Youtube

sonicboom
Author by

sonicboom

Updated on September 18, 2022

Comments

  • sonicboom
    sonicboom over 1 year

    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)' [email protected]
    

    chkrootkit.sh

    #!/bin/bash
    chkrootkit | mail -s "chkrootkit Daily Run (my server)" [email protected]
    

    logwatch.sh

    #!/bin/sh
    (
    logwatch
    ) | mail -s 'logwatch Daily Log (my server)' [email protected]
    

    I replaced [email protected] 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?

    • Turgut Kalfaoglu
      Turgut Kalfaoglu over 7 years
      Make sure that the files you created in cron.daily/weekly/hourly/etc are executable just do a chmod +x /etc/cron.daily/whatever
    • WineSoaked
      WineSoaked about 4 years
      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.
  • sonicboom
    sonicboom over 10 years
    So do i just put the filename in there?
  • Jesus Ibarra
    Jesus Ibarra over 10 years
    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.
  • sonicboom
    sonicboom over 10 years
    So to run once a day at 10PM i would put * 22 * * * test > rkhunter.sh right?
  • sonicboom
    sonicboom over 10 years
    ah awesome! ill try it right now!
  • Jesus Ibarra
    Jesus Ibarra over 10 years
    * 22 * * * /home/sonicboom/somedirectory/yourscript.sh | mail -s "Your script started execution" [email protected]
  • sonicboom
    sonicboom over 10 years
    Got it! Let me try now!
  • sonicboom
    sonicboom over 10 years
    So i put this in but it didnt work i didnt get an email or anything, and i also put PATH=/usr/sbin:/usr/bin:/sbin:/bin to the top 26 4 * * * /etc/cron.daily/rkhunter.sh | mail -s "Your script started execution" [email protected]
  • sonicboom
    sonicboom over 10 years
    And this is what i saw in my log. Aug 27 04:26:01 localhost CRON[2718]: (root) CMD (/etc/cron.daily/rkhunter.sh | mail -s "Your script started execution" [email protected])
  • sonicboom
    sonicboom over 10 years
    ah i think it worked now.
  • sonicboom
    sonicboom over 10 years
    Yeah took a few minutes
  • lepe
    lepe about 9 years
    +1 Isn't anacron installed by default? I would have expected that. I think that would solve it for me. Thanks.
  • Natim
    Natim about 9 years
    Yes I don't know what happened either since it is a package usually installed at startup.
  • Albert Català
    Albert Català almost 8 years
    This is the answer I was looking for, after various test, I realized that another script file without sh extension is executed
  • TalkLittle
    TalkLittle about 7 years
    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.
  • northern-bradley
    northern-bradley almost 7 years
    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"
  • kiwicomb123
    kiwicomb123 over 5 years
    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"
  • Natim
    Natim over 4 years
    So maybe it was because run-parts didn't work? In anycase installing anacron fixed it for me.
  • Natim
    Natim over 4 years
  • realtebo
    realtebo over 4 years
    Which files are these? is it the content of /etc/logrotate.d folder ?