Enhancing Skills

crontab: Schedule jobs (commands) to run automatically at specified times

Command: crontab

The cron daemon is used to schedule commands or scripts to execute automatically at specified intervals. Jobs are defined in a crontab file, which outlines the timing and commands to run.


Commands and Outputs:

  • Command: crontab -e Description: Edit the crontab file for the current user, where you define jobs and their schedules. Sample Command and Output:
  $ crontab -e

Text Editor Output:

  # Edit this file to introduce tasks to be run by cron.
  # Each task to run has to be defined through a single line.
  # Example:
  # .---------------- minute (0 - 59)
  # |  .------------- hour (0 - 23)
  # |  |  .---------- day of month (1 - 31)
  # |  |  |  .------- month (1 - 12)
  # |  |  |  |  .---- day of week (0 - 6) (Sunday=0)
  # |  |  |  |  |
  # *  *  *  *  *  command_to_be_executed

  # Run a script every day at 3 AM
  0 3 * * * /path/to/script.sh

  # Run a command every Monday at 5 PM
  0 17 * * 1 /usr/bin/somecommand

  • Command: crontab -l Description: Display the current user’s crontab, listing all scheduled jobs. Sample Command and Output:
  $ crontab -l

Output:

  # Display the current user's crontab
  0 3 * * * /path/to/script.sh
  0 17 * * 1 /usr/bin/somecommand

  • Command: crontab -r Description: Remove the current user’s crontab, deleting all scheduled jobs. Sample Command and Output:
  $ crontab -r

Output:

  # No output if successful, removes the current user's crontab

  • Command: sudo crontab -e Description: Edit the system-wide crontab for the root user, allowing for scheduling jobs with root privileges. Sample Command and Output:
  $ sudo crontab -e

Text Editor Output:

  # Edit this file to introduce tasks to be run by cron for root.
  0 1 * * * /path/to/root_script.sh

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.