How to use the crontab command in Linux?
In Linux, the crontab command is used to create, edit, and manage a user’s scheduled tasks. Here is a basic usage of the crontab command:
- Create a new schedule/calendar for tasks:
crontab -e
This will open a text editor that contains the scheduled task list for the current user (if any). If this is the first time using the crontab command, it may be an empty file.
- Edit scheduled tasks:
In the text editor that is open, each line represents a scheduled task. The format of each line is as follows:
分钟(0-59) 小时(0-23) 日期(1-31) 月份(1-12) 星期(0-7) 命令
For example, to run a script at 8 o’clock every morning, you can add the following line:
0 8 * * * /path/to/script.sh
Save and exit the editor.
- View the list of scheduled tasks:
crontab -l
This will list all the scheduled tasks for the current user.
- Remove scheduled task.
crontab -r
This will remove all scheduled tasks of the current user.
- Other commonly used options:
crontab -u username
This will allow editing the user’s scheduled tasks by specifying their username.
crontab -e -u username
This will allow the creation of a new schedule table by specifying a username.
crontab -l -u username
This will list the scheduled tasks for the specified username.
These are some basic crontab command usages. You can refer to the more detailed help documentation by using the man crontab command.