47

I have been digging through my Linux system. To try and understand how it all works

In the /etc/crontab file. I see the following

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

What is run-parts, what does it do, and how can I use it.

Dave M
  • 13,138
  • 25
  • 36
  • 47
nelaaro
  • 13,149
  • 30
  • 84
  • 111

1 Answers1

59

Basically, run-parts(8) takes a directory as an argument.

It will run every script that is found in this directory. For example, if you do a listing of /etc/cron.hourly, you'll see that it's a directory where you can put executable files to be run every hour.

As you can see, in cron it's used for convenience, since you only have to specify one directory and everything in that directory will be executed. This makes it easy to maintain scripts in one of the etc/cron* directories.

See its manpage for more options that could be exploited for your own use cases. You could for example do a simple check and show which scripts would be run:

run-parts -v --test /etc/cron.hourly

The -v flag might not be available everywhere.

tripleee
  • 3,121
  • 5
  • 32
  • 35
slhck
  • 223,558
  • 70
  • 607
  • 592
  • 1
    What is the `root` portion for? – Jake N Nov 09 '12 at 12:59
  • 1
    @jakenoble `root` means that the command (`run-parts` in this case) will be run as root user – Stefan Haberl Aug 30 '13 at 09:36
  • 7
    Note that on CentOS (at least el5) run-parts is a bash script that doesn't have any options so you will get "-v is not a directory". Or at least that's what it shown me on my system. – Nux Dec 17 '13 at 14:03
  • CentOS probably got the idea from Debian but reimplemented it independently. There are many Linux distros which do not have this mechanism, too. – tripleee Jan 12 '23 at 10:24