Centos – Script in /etc/cron.hourly/ never running

centoscron

I have a script in /etc/cron.hourly :

-rwxr-xr-x 1 root root 85 Dec  6 19:05 /etc/cron.hourly/nvidia_to_exclusive

containing (with an empty line at the end):

#!/bin/bash 

/usr/bin/nvidia-smi -c 1 > /home/user/nvidia-smi_set_exclusive.log

The script isn't executed by cron at all, even if using run-parts /etc/cron.hourly successfully execute it.

What could be missing ?

Best Answer

The problem was that the cron service was inactive.

While I'm here, I'll summarize all the steps I've found to make a script in /etc/cron.hourly/ work :

  • Check that the name of your script is only using valid characters for run-parts, i.e. [a-zA-Z0-9_-].
    So don't use extension like .sh.
  • Check that your script is executable.
    If not : chmod +x /etc/cron.hourly/yourScript
  • Check that your script contains the shebang at the top (#!/bin/bash for example).
  • Check that your script runs with run-parts :
    run-parts --test /etc/cron.hourly → your script should be printed.
    run-parts /etc/cron.hourly→ your script should be executed.
    You can check at the end of /var/log/cron if your script successfully finished.
  • Check that cron is running with service crond status.
    If not : service crond stop then service crond start
  • Check if your /var/log/cron contains the error BAD FILE MODE (/etc/cron.d/0hourly).
    If it's the case, you probably need to execute chmod 0644 /etc/cron.d/0hourly (cron does not like this file to be executable).
  • Check - at least by default on CentOS 7 - that /etc/cron.d/0hourly exists and contains the line
    01 * * * * root run-parts /etc/cron.hourly
Related Question