/etc/cron.d and anacron
The system cron directories such as /etc/cron.d and /etc/cron.daily provide a mechanism for packages and system administrators to add jobs. They operate independently from per-user crontab files, allowing jobs to be managed across multiple files. anacron is a daemon that catches up on periodic tasks that were missed while the machine was powered off, running them at the next startup. It is especially useful for scheduled backups on machines that are not always on, rather than servers.
Syntax
# ----------------------------------------------- # Job file format for /etc/cron.d/ # ----------------------------------------------- # Format: minute hour day month weekday run-as-user command # The user column is required, unlike a user crontab # min(0-59) hour(0-23) day(1-31) month(1-12) weekday(0-7, 0 and 7 = Sunday) user command 0 2 * * * root /usr/local/bin/cleanup.sh # ----------------------------------------------- # Directory format for /etc/cron.daily/ etc. # ----------------------------------------------- # Simply place an executable script in the directory to register a job # Files with a dot (.) or tilde (~) in the name are not executed # Execution timing is controlled by /etc/crontab or /etc/anacrontab # ----------------------------------------------- # Format of /etc/anacrontab # ----------------------------------------------- # Format: period(days) delay(minutes) job-id command # Environment variables such as SHELL and PATH can also be set SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # period(days) delay(min) job-id command 1 5 cron.daily run-parts --report /etc/cron.daily 7 10 cron.weekly run-parts --report /etc/cron.weekly @monthly 15 cron.monthly run-parts --report /etc/cron.monthly
Reference
| Directory / File | Description |
|---|---|
/etc/cron.d/ | A directory where packages and system administrators place job files. Jobs can be grouped per file, making management easier. The user column is required. |
/etc/cron.daily/ | A directory for scripts you want to run once a day. Simply place the script here with execute permission to register it. |
/etc/cron.weekly/ | A directory for scripts you want to run once a week. Execution timing is controlled by /etc/anacrontab. |
/etc/cron.monthly/ | A directory for scripts you want to run once a month. |
/etc/crontab | The system-wide cron configuration file. Unlike a user crontab, a user column is required. It can be edited directly, but splitting jobs into /etc/cron.d/ is recommended. |
/etc/anacrontab | The anacron configuration file. Jobs are defined in four columns: period (days), delay (minutes), job ID, and command. Jobs missed while the machine was off are run at the next startup. |
anacron | A daemon that reads /etc/anacrontab at system startup and runs any job for which the specified number of days has passed since its last execution. Suitable for machines that are not always running. |
run-parts | A command that runs all executable files in a specified directory. Used to execute the scripts in /etc/cron.daily/ and similar directories. |
/var/spool/anacron/ | The directory where anacron records the last execution date of each job. This timestamp is used to determine whether a catch-up run is needed. |
Examples
/etc/cron.d/shimotsuki-backup
# ----------------------------------------------- # Example of registering a system job in /etc/cron.d/ # Backs up Shimotsuki Mika's home directory at 2 AM every night # ----------------------------------------------- # Set environment variables (a minimal environment is used if omitted) SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # Job format: minute hour day month weekday run-as-user command # Run the backup script as root at 02:00 every day 0 2 * * * root /usr/local/bin/shimotsuki-backup.sh >> /var/log/shimotsuki-backup.log 2>&1 # Run an incremental backup integrity check as root at 03:00 every Monday 0 3 * * 1 root /usr/local/bin/backup-verify.sh
Run the following command:
$ sudo chmod 644 /etc/cron.d/shimotsuki-backup $ ls -l /etc/cron.d/ total 24 -rw-r--r-- 1 root root 88 Mar 25 09:00 .placeholder -rw-r--r-- 1 root root 312 Mar 25 09:01 shimotsuki-backup -rw-r--r-- 1 root root 102 Mar 10 00:00 sysstat
/etc/cron.daily/kogami-logrotate
#!/bin/bash
# -----------------------------------------------
# Example script to place in /etc/cron.daily/
# Rotates Kogami Shinya's log files every day
# -----------------------------------------------
LOG_DIR="/var/log/kogami-monitor"
KEEP_DAYS=30
# Delete log files older than 30 days
find "${LOG_DIR}" -name "*.log" -mtime +${KEEP_DAYS} -delete
# Compress and archive today's log files
ARCHIVE="${LOG_DIR}/archive-$(date +%Y%m%d).tar.gz"
find "${LOG_DIR}" -maxdepth 1 -name "*.log" -exec tar -czf "${ARCHIVE}" {} +
echo "Log rotation complete: ${ARCHIVE}"
Run the following command:
$ sudo chmod +x /etc/cron.daily/kogami-logrotate $ sudo run-parts --test /etc/cron.daily/ /etc/cron.daily/apt-compat /etc/cron.daily/kogami-logrotate /etc/cron.daily/logrotate /etc/cron.daily/man-db
/etc/anacrontab
# ----------------------------------------------- # Example anacron configuration file # Catches up on jobs that were missed while the machine was off # ----------------------------------------------- SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # Set MAILTO to receive job results by email MAILTO=ginoza@psycho-pass.example # Format: period(days) delay(min) job-id command # Daily job (runs 5 minutes after startup) 1 5 cron.daily run-parts --report /etc/cron.daily # Weekly job (runs 10 minutes after startup) 7 10 cron.weekly run-parts --report /etc/cron.weekly # Monthly job (runs 15 minutes after startup) @monthly 15 cron.monthly run-parts --report /etc/cron.monthly # ----------------------------------------------- # Ginoza Nobuchika's system health check (every 3 days) # Runs if 3 or more days have passed since the last execution # ----------------------------------------------- 3 20 ginoza-healthcheck /usr/local/bin/ginoza-healthcheck.sh
Run the following command:
$ sudo anacron -T $ echo $? 0 $ cat /var/spool/anacron/cron.daily 20260324
Command examples (manual execution and status check for anacron)
# ----------------------------------------------- # Verifying and manually running anacron # ----------------------------------------------- # Check the syntax of the anacron configuration file (exit code 0 means no errors) sudo anacron -T # Run all pending jobs immediately (useful for testing) sudo anacron -f -d # Check the last-run timestamp cat /var/spool/anacron/cron.daily cat /var/spool/anacron/cron.weekly # Check the anacron unit status in a systemd environment (Debian/Ubuntu) systemctl status anacron.service # Run the scripts in /etc/cron.daily/ manually for testing sudo run-parts --verbose /etc/cron.daily/
Run the following command:
$ sudo anacron -f -d Anacron 2.3: invoked on 2026-03-25 Will run job `cron.daily' in 5 min. Will run job `ginoza-healthcheck' in 20 min. Jobs will be executed sequentially
Overview
For managing system cron jobs, splitting files into /etc/cron.d/ is recommended over writing directly to /etc/crontab. Packages can automatically register jobs at install time, and each job can be managed in its own file, making change history easier to track. The /etc/cron.daily/ and similar directories are even simpler — just place an executable script there to register it. Note that files with a dot (.) or tilde (~) in the name are ignored by run-parts.
anacron records the last execution date for each job in /var/spool/anacron/, and runs any job at the next startup if the specified number of days has elapsed. This ensures that scheduled backups run reliably even on laptops or desktops that are shut down at night. For scheduling tasks as an individual user, see the crontab page. For more fine-grained schedule control and dependency management, see systemd timer as well.
If you find any errors or copyright issues, please contact us.