
Crontabs are a powerful way to schedule and automate tasks in Linux. In this guide, we’ll cover how to create, manage, and remove scheduled tasks using cron.
Viewing Your Cron Table
To view your existing cron table, use the following command:
crontab -l
Setting Your Default Editor
You can choose your preferred text editor for working with crontabs. For example, to make nano
your default editor, run:
export EDITOR=/usr/bin/nano
Creating or Editing Your Cron Table
To create or edit your crontab, use:
crontab -e
Cron Format
Cron expressions follow a specific format:
* * * * * command_to_execute
*
: Wildcard, meaning “every.”min
: Minute (0 – 59).hr
: Hour (0 – 23).day
: Day of the month (1 – 31).mnth
: Month (1 – 12).dayofweek
: Day of the week (0 – 7, where both 0 and 7 represent Sunday).
Examples
- To execute a command at 01:32 on the 17th of January with no day-of-week:
32 01 17 01 * command_to_execute
- To execute a command at 20:14 every Monday in March:
14 20 * 03 1 command_to_execute
- Days can also be expressed as abbreviations e.g. ‘mon’ instead of ‘1’, making the command more readable:
14 20 * 03 mon command_to_execute
Running a Script
You can also set the path to a script in your crontab. For example:
32 01 17 01 * /path/to/your/script.sh
Make sure the script is executable (chmod +x /path/to/your/script.sh
).
Removing a Crontab
You can remove your crontab with:
crontab -r
Editing Another User’s Crontab
To edit another user’s crontab, use sudo
and specify the user:
sudo crontab -u root -e
Or, without sudo
:
crontab -u apache
Leave a Reply