8

For some reason my crontab is not running the hourly/weekly/etc scripts. It seems to be that run-parts is not working and I cant get it to work on my local machine as well. Is there a mistake in the way I'm using it?

fabe@fabetop ~ $ cat /home/fabe/tmp/test.sh 
#!/bin/sh
touch /home/fabe/tmp/test_it
fabe@fabetop ~ $ ls -la /home/fabe/tmp
total 32
drwx------  2 fabe fabe  4096 Feb 20 15:00 .
drwx------ 60 fabe fabe 16384 Feb 20 15:00 ..
-rwxr-xr-x  1 fabe fabe    39 Feb 20 15:00 test.sh
fabe@fabetop ~ $ run-parts --report /home/fabe/tmp
fabe@fabetop ~ $ ls -la /home/fabe/tmp
total 32
drwx------  2 fabe fabe  4096 Feb 20 15:00 .
drwx------ 60 fabe fabe 16384 Feb 20 15:00 ..
-rwxr-xr-x  1 fabe fabe    39 Feb 20 15:00 test.sh
  • What crontab? You have no crontab here. Why are you using run-parts anyway? Why not just run the script directly? – terdon Feb 20 '14 at 15:49
  • I am not showing the crontab as i have isolated the problem to this command. As to why run-parts I have many users one for each site and I just use folders in there /home/username/cron/hourly etc which is more convenient then having to edit the crontab all the time (just update the git project and they will run...) – Fabian Barkhau Feb 20 '14 at 16:15
15

The problem is the name of your script. From man run-parts:

If neither the --lsbsysinit option nor the --regex option is given then the names must consist entirely of ASCII upper- and lower-case letters, ASCII digits, ASCII underscores, and ASCII minus- hyphens.

In other words, no extension. Oddly enough, even with the --lsbsysinit option, you can't specify a file like foo.sh since that matches none of the namespaces covered:

If the --lsbsysinit option is given, then the names must not end in .dpkg-old or .dpkg-dist or .dpkg-new or .dpkg-tmp, and must belong to one or more of the following namespaces: the LANANA assigned namespace (^[a-z0-9]+$); the LSB hierarchical and reserved namespaces (^_?([a-z0-9_.]+-)+[a-z0-9]+$); and the Debian cron script namespace (^[a-zA-Z0-9_-]+$).

So, while foo.sh fails, foo.s-h or foo.-sh will work. I have no idea why they've done it this way but presumably they are following some standard or other.

Anyway, you have 2 options, either rename your scripts to not have an extension (extensions are optional in *nix anyway) or you can skip using run-parts altogether. Use this in your crontab instead:

find /home/fabe/tmp/ -prune -type f -executable -exec {} \;

The command above will find all executable files in the target directory and run them. I think that -executable is a GNU extension but you have tagged this as Linux so I assume you have GNU find.

  • The is very strange as on RHEL there is a file called 0yum-hourly.cron which runs with run-parts – Ray Foss Jun 25 '18 at 14:09
  • THANK YOU! I was pulling my hair to try to understand why the "busybox" version of run-parts refused to find my "test.sh" script. Renamed it to "test" and now it works. Just insane. – Daniele Testa Jul 9 at 17:41
3

Every script placed in folder /etc/cron.hourly would run on hourly basis.

However your files needs to be:

  • executable,
  • match the Debian cron script namespace (^[a-zA-Z0-9_-]+$).

So for example if you've script with extension (.sh in this case), it won't work.

To print the names of the scripts which would be run, try:

sudo run-parts --report --test /etc/cron.hourly

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.