Skip to content

crontab Linux command

The crontab command in Linux allows users to schedule tasks to run at specified times automatically. Users can create, edit, list, and remove cron jobs with crontab. Each cron job is a command or script that will be executed at the specified time and frequency. By using the crontab command, users can automate repetitive tasks, such as backups, updates, and maintenance, without manual intervention. Managing cron jobs with crontab provides flexibility and efficiency in task scheduling, enabling users to streamline their workflow and improve productivity.

crontab Syntax:

Terminal window
crontab [ -u user ] file
crontab [ -u user ] { -l | -r | -e }

Options:

OptionDescription
-uDefine the user whose crontab is edited

Parameters:

ParameterDescription
fileUse the contents of file to replace the current crontab

crontab bash Examples:

Run a Script Every Hour

Terminal window
0 * * * * /path/to/script.sh

Schedules the script to run every hour at the 0th minute.

Run a Command Every Day at Midnight

Terminal window
0 0 * * * command_to_run

Executes the specified command every day at midnight.

Run a Script Every Monday

Terminal window
0 0 * * 1 /path/to/script.sh

Schedules the script to run every Monday at midnight.

Run a Script Every 30 Minutes

Terminal window
*/30 * * * * /path/to/script.sh

Executes the script every 30 minutes throughout the day.

Remove Old Backup Files Every Sunday

Terminal window
0 0 * * 0 find /path/to/backup/* -mtime +7 -exec rm {} \;

Deletes backup files older than 7 days every Sunday at midnight.

Send an Email Notification Every Weekday

Terminal window
0 9 * * 1-5 echo "Don't forget the meeting today!" | mail -s "Meeting Reminder" [email protected]

Sends an email reminder about the meeting at 9 AM for every weekday.

How do I use crontab in Linux?

To use the crontab command in Linux, execute the following command:

Terminal window
crontab -e

How do I list all crontab jobs in Linux?

You can list all crontab jobs in Linux by running the following command:

Terminal window
crontab -l

How do I edit a crontab file in Linux?

To edit a crontab file in Linux, run the following command:

Terminal window
crontab -e

How do I remove all crontab jobs in Linux?

To remove all crontab jobs in Linux, use the following command:

Terminal window
crontab -r

How do I schedule a daily cron job in Linux?

To schedule a daily cron job in Linux, you can use the following syntax to run a command at a specific time every day:

Terminal window
0 0 * * * /path/to/command

How do I schedule a cron job to run every hour in Linux?

To schedule a cron job that runs every hour in Linux, use the following crontab syntax:

Terminal window
0 * * * * /path/to/command

How do I schedule a cron job to run on specific days of the week in Linux?

To schedule a cron job that runs on specific days of the week in Linux, use the following syntax:

Terminal window
0 0 * * 1,4 /path/to/command

How do I redirect output to a file for a cron job in Linux?

To redirect output to a file for a cron job in Linux, use the following format in your crontab entry:

Terminal window
0 0 * * * /path/to/command > /path/to/output.log 2>&1

Applications of the crontab command

  • Scheduling tasks to run periodically
  • Automating system maintenance tasks
  • Sending regular reports or notifications
  • Updating software or database backups
  • Monitoring system performance
  • Running scripts at specific times
  • Automating data synchronization
  • Performing routine actions on a schedule