How do you use the run-parts command?
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
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.
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