Linux / How to Use Crontab
In Linux, using crontab allows you to repeatedly execute commands or scripts at specified times. With crontab -e, you can edit the current user's scheduled tasks, entering five time fields and the command to execute in a single line.
Difference Between cron and crontab
cron is a service that regularly checks and executes scheduled tasks. crontab is a list of tasks recording execution times and commands, generally managed separately for each user. The examples in this article are based on the typical user crontab of Debian and Ubuntu series. Installation methods, service names, and supported features may differ depending on the distribution and cron implementation.
Scheduled tasks are executed with the owner privileges of the corresponding crontab. If privileges not held by the current user are required, carefully set the administrator and execution account, and do not create tasks with unnecessarily broad privileges. Tasks scheduled for a time when the computer is off or the cron service is stopped will not be executed.
Checking and Editing Crontab
In the terminal, use the following command to check the scheduled tasks of the current user. If a message appears saying that there is no list, it means that this user does not have a crontab yet.
crontab -l
To add or modify tasks, run the following command. The editor selection screen may appear the first time you run it. After saving the file and exiting the editor, cron will check the syntax and install the new list. If an error appears, edit it again according to the instructions and confirm that the installation message is displayed.
crontab -e
Scheduled tasks for the administrator account are separate from the current user. Only edit root's list with sudo crontab -e and check it with sudo crontab -l when administrative privileges are required. Do not confuse the two lists.
To remove a single task, delete only that line with crontab -e and save it. crontab -r deletes the entire list of the current user's scheduled tasks, so check the list before using it and back it up in a safe location if necessary. You can back it up with crontab -l > ~/crontab-backup.txt, and if restoration is needed, reinstall the entire file with crontab ~/crontab-backup.txt. Restoration will overwrite the current list, so check for any changes first.
Format of scheduled time
Each job in a user crontab consists of five time fields followed by the command. In system crontab files such as /etc/crontab or /etc/cron.d/, an additional execution user field is added between the time fields and the command, so do not copy the format below as is.
minute hour day-of-month month day-of-week command
| Field | Allowed Range | Meaning |
|---|---|---|
| minute | 0–59 | Minute |
| hour | 0–23 | Hour |
| day-of-month | 1–31 | Date |
| month | 1–12 | Month |
| day-of-week | 0–7 | Day of the week. 0 and 7 are Sunday |
* refers to all values in the field. A comma specifies multiple values (10,20), a hyphen specifies a range (1-5), and a slash specifies intervals within a range (*/10). For example, */10 in the minute field means 0, 10, 20, 30, 40, 50 minutes every hour. Intervals are interpreted within each field, so they do not always guarantee the same spacing based on elapsed time.
If both date and day of the week are restricted, a Debian cron will run on a day that meets either condition. For example, 0 4 1 * 1 runs at 4 a.m. every 1st day of the month and every Monday, not only when the 1st is a Monday. You can check detailed formats in the system's man 5 crontab or Debian cron crontab format documentation.
Scheduled job examples
Replace /home/user/bin/backup.sh in the following example with the absolute path of the actual script. The user must have access to the script and target files, and if running the script directly, execute permissions and the correct interpreter specification are also needed. First, run it in the terminal with the same account to verify the results.
0 4 * * * /home/user/bin/backup.sh
0 4 * * 1-5 /home/user/bin/backup.sh
*/10 4 * * * /home/user/bin/backup.sh
The first line runs every day at 4 a.m., the second line runs Monday through Friday at 4 a.m., and the third line runs every 10 minutes from 4 a.m. to 4:50 a.m. The examples are independent schedules. When all are entered, all tasks on each line will be registered.
Register and test a job safely
When practicing, instead of an actual backup, try using a task that leaves a record in the current user's home directory.
- Run
crontab -e, add the following single line, then save and exit. The date and time will be appended to the file every minute.
* * * * * /bin/date >> "$HOME/cron-test.log" 2>&1
- Run
crontab -lto check the installed entry. After 1–2 minutes, runcat ~/cron-test.logand check that a record has been added. - Once confirmed, delete only the practice entry with
crontab -eand save. You may delete the log file after checking its contents.
If the practice does not work, check whether the cron service is installed and running. In a typical Ubuntu cron package, you can view the status with systemctl status cron. The service name and log location vary by distribution, and in an Ubuntu environment using the systemd journal, you can check the service logs with journalctl -u cron.
Points to Check When it Does Not Run
cron may run in an environment different from the login shell. Specify commands and scripts with absolute paths whenever possible, and clearly define required environment variables and working directories in the script. To check for errors, you can also redirect the job's output and errors to a log file that the user can access. Since logs may contain sensitive information, be careful with access permissions and retention periods.
Check whether the system time and timezone are as expected, whether the execution account can access the files, and whether there is a possibility of overlapping executions. The % used in commands is treated specially by cron, so to pass it literally, it usually needs to be escaped with \%. Check the documentation of the cron you are using for exact behavior.




