In system administration, there is a powerful tool to schedule tasks: crontab. With crontab, you can run scripts and commands at specific dates and times. It is very useful in Unix-based systems. To use it well, you need to know how to edit a crontab file. This guide will explain the basics.
Understanding Crontab
The word “crontab” means “cron table”. It’s the file used by the cron daemon. The cron daemon is a tool in Linux that runs tasks on your system at scheduled times. The schedule is set by the crontab file, which is a simple text file with a list of commands and when to run them. Each line in a crontab file has a specific format for scheduling tasks.
Accessing the Crontab File
To open your user-specific crontab file, use the command crontab -e. This will open the crontab file for your user account in the default text editor. If you want to use a different editor, you can set the VISUAL or EDITOR environment variable.
Crontab File Syntax
Understanding the syntax is key to editing a crontab file. Each line in the file is a single cron job and follows this format:
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of the week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of the month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
An asterisk (*) in a field means “every value,” like “every minute” or “every day”.
Editing the Crontab File
In the crontab file, you can add tasks using the syntax above. For example, to run a backup script every day at 3 am, add this line:
0 3 * * * /path/to/yourscript.sh
Save and close the file, and the cron daemon will start using the new schedule.
Common Crontab Commands
Here are some common crontab commands:
- `crontab -e`: Edit your crontab file.
- `crontab -l`: Show the contents of your crontab file.
- `crontab -r`: Remove your current crontab.
- `crontab -i`: Prompt before removing a crontab.
Crontab Special Strings
Crontab also supports special strings to replace the five time-and-date fields:
- `@reboot`: Run once, at startup.
- `@yearly`: Run once a year, "`0 0 1 1 *`".
- `@annually`: Same as `@yearly`.
- `@monthly`: Run once a month, "`0 0 1 * *`".
- `@weekly`: Run once a week, "`0 0 * * 0`".
- `@daily`: Run once a day, "`0 0 * * *`".
- `@hourly`: Run once an hour, "`0 * * * *`".
These special strings make the crontab file easier to read and manage.
Handling Output
By default, cron sends an email with the output of each job. To change where the output goes, use shell redirection:
30 2 * * * /path/to/yourscript.sh > /path/to/outputfile 2>&1
The > sends the script's output to a file, and 2>&1 sends error output to the same place as the standard output.
Final Thoughts
Editing a crontab file to schedule tasks on a Unix-based system is important for any system administrator or developer. With a basic understanding of the syntax, scheduling tasks can be easy and efficient.
Always test your cron jobs to make sure they run as expected. With careful setup and testing, you can automate many tasks, allowing you to focus on more important things.