LinuxCommandLibrary

run-parts

Execute scripts in a directory sequentially

SYNOPSIS

run-parts [options] directory

PARAMETERS

directory
    The path to the directory containing the scripts to be executed. This is a mandatory argument.

--test
    Simulate execution. Only print the names of scripts that would be run, without actually executing them.

--list
    List only. Similar to --test, but specifically implies listing for human review.

--report
    Report progress. Print the name of each script before and after its execution, along with its exit status.

--reverse
    Reverse order. Run scripts in reverse lexicographical order. By default, they are run in forward lexicographical order.

--regex=REGEX
    Filter by regular expression. Only run files whose names match the provided extended regular expression REGEX. This overrides the default filename filtering.

--verbose
    Enable verbose output. Print more detailed information about the execution process.

--exit-on-error
    Exit on script failure. If any script exits with a non-zero status, run-parts will immediately terminate without running subsequent scripts.

--return-error-on-failure
    Return non-zero if any script fails. The run-parts command itself will return a non-zero status if any executed script returns a non-zero status, even if it continues to run subsequent scripts.

DESCRIPTION

run-parts is a utility designed to execute all executable files found within a specified directory. It's commonly used by system daemons like cron to automate the execution of recurring tasks. For instance, the /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly directories often contain scripts that run-parts iterates through and executes regularly.

A key feature of run-parts is its default filename filtering: it only executes files whose names consist entirely of alphanumeric characters, underscores, and hyphens. This prevents it from running temporary files, backups, or configuration files that might reside in the same directory but are not intended for execution. It also supports various options for filtering by file extension, running in reverse order, reporting errors, and more, making it a flexible tool for managing automated system tasks.

CAVEATS

By default, run-parts has strict filename filtering (alphanumeric, hyphens, underscores), which can lead to unexpected omissions if not understood. Its default execution order is lexicographical. Errors in one script do not stop subsequent scripts from running unless --exit-on-error is specified. It runs scripts sequentially, not in parallel.

DEFAULT FILENAME FILTERING

By default, run-parts will only execute files in the specified directory whose names consist entirely of alphanumeric characters, underscores, and hyphens. This strict filtering mechanism prevents the accidental execution of backup files (e.g., script.bak), temporary files, or other non-executable content. This default behavior can be overridden using options like --regex for more flexible pattern matching.

EXIT STATUS

run-parts exits with status 0 if all scripts execute successfully or if no scripts are found. If any script returns a non-zero exit status, run-parts will typically still return 0 by default, unless the --return-error-on-failure or --exit-on-error options are specified. Understanding this default behavior is crucial for scripting robust error handling.

HISTORY

run-parts has been a fundamental utility in Debian and other Unix-like systems for automating routine administrative tasks since the early days of Linux, specifically for managing the /etc/cron.* directories. Its design reflects the need for a simple, robust way to execute a collection of scripts, relying on cron for scheduling. Its core functionality has remained largely consistent, with new options added over time to enhance flexibility, such as regex filtering and improved error handling.

SEE ALSO

cron(8), crontab(5), anacron(8), systemd-run(1), systemd.timer(5)

Copied to clipboard