6月21日 21:41

What are the time format, common commands, and best practices for Linux scheduled tasks cron?

Linux scheduled tasks (cron) are an important tool for automated operations, allowing scripts and commands to be executed regularly.

cron basic concepts:

  • cron is the scheduled task scheduler for Linux systems
  • The cron daemon (crond) is responsible for executing scheduled tasks
  • Configuration files: /etc/crontab (system-level), /var/spool/cron/username (user-level)
  • Time format: minute hour day month week command

Time format explanation:

  • minute (0-59): which minute of the hour
  • hour (0-23): which hour of the day
  • day (1-31): which day of the month
  • month (1-12): which month of the year
  • week (0-7, 0 and 7 both represent Sunday): which day of the week

Special symbols:

  • *: match all values
  • ,: separate multiple values, e.g., 1,3,5 means 1st, 3rd, 5th
  • -: indicate range, e.g., 1-5 means 1 to 5
  • /n: indicate interval, e.g., */5 means every 5 units

Common examples:

  • Execute every minute: * * * * * command
  • Execute every hour: 0 * * * * command
  • Execute at 2 AM every day: 0 2 * * * command
  • Execute at 3 AM every Monday: 0 3 * * 1 command
  • Execute at 4 AM on the 1st of every month: 0 4 1 * * command
  • Execute every 5 minutes: */5 * * * * command
  • Execute at 9 AM every weekday (Monday to Friday): 0 9 * * 1-5 command

crontab commands:

  • crontab -e: edit current user's scheduled tasks
  • crontab -l: list current user's scheduled tasks
  • crontab -r: delete all scheduled tasks for current user
  • crontab -u username -e: edit specified user's scheduled tasks (requires root privileges)

System-level scheduled tasks:

  • /etc/crontab: system-level scheduled task configuration file
  • /etc/cron.hourly/: directory for tasks executed every hour
  • /etc/cron.daily/: directory for tasks executed every day
  • /etc/cron.weekly/: directory for tasks executed every week
  • /etc/cron.monthly/: directory for tasks executed every month

Environment variables:

  • Environment variables when cron tasks execute are different from when user logs in
  • Environment variables can be defined in crontab file
  • It is recommended to use absolute paths in scripts
  • Can source environment variable files at the beginning of scripts: source ~/.bashrc

Logs and debugging:

  • cron log location: /var/log/syslog or /var/log/cron
  • View logs: grep CRON /var/log/syslog
  • Debugging tips: redirect output to log file: command >> /tmp/cron.log 2>&1
  • Send email: cron sends output to user's mailbox by default

Best practices:

  • Use absolute paths
  • Set correct environment variables in scripts
  • Add logging for easier debugging
  • Use lock files to prevent duplicate execution
  • Test scripts before adding to cron
  • Set reasonable execution times to avoid system peak hours
  • Regularly check cron task execution

Common problems:

  • Insufficient script permissions: ensure script has execute permission (chmod +x script.sh)
  • Environment variable issues: explicitly set environment variables in scripts
  • Path issues: use absolute paths or switch to correct directory
  • Timezone issues: ensure system timezone is correct
  • cron service not started: systemctl status cron or systemctl start cron

anacron (asynchronous cron):

  • Used for systems that don't run 24 hours
  • Configuration file: /etc/anacrontab
  • Suitable for devices like laptops that are not always on
  • Checks last execution time and makes up if missed

systemd timers:

  • systemd's scheduled task alternative
  • Configuration files: /etc/systemd/system/*.timer
  • View timers: systemctl list-timers
  • More flexible scheduling options
  • Better log integration
标签:Linux