Linux crontab Command Format and example
Linux crontab Command Format and example
Run */5 * every five minutes ****
0 *** per hour ****
Execute 0 0 every day ***
Execute 0 0 every week ** 0
0 0 1 per month **
Execute 0 0 1 1 * every year *
For detailed usage, refer
1. Role
You can use the crontab command to modify the crontab configuration file. Then, the configuration is executed by the cron utility at an appropriate time. This command has the permission of all users.
2. Format
Crontab [-u user] {-l |-r |-e}
3. Main Parameters
-E: run the text editor to set the time table. The empty text editor is vi.
-R: Delete the current time table.
-L list the current time table.
The format of the Crontab file is m h d m d cmd ". M indicates the minute (0 ~ 59), H indicates the hour (0 ~ 23), D represents the Day (1 ~ 31), m represents the month (1 ~ 12), d indicates the day in a week (0 ~ 6, 0 is Sunday ). Cmd indicates the program to run. It is sent to sh for execution. The shell contains only three environment variables: USER, HOME, and SHELL.
Use the cron service and use the server crond status to view the cron service status. If it is not started, the service crond start starts it. The cron service is a scheduled service, you can use the crontab command to add or edit tasks that require scheduled execution:
Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.
Crontab-l // list the details of a user's cron Service
Crontab-r // delete a user's cron Service
Crontab-e // edit a user's cron Service
Crontab filename // use filename as the crontab task list file and load it
For example, to view your cron settings as root: crontab-u root-l
For another example, root wants to delete fred's cron settings: crontab-u fred-r
When editing the cron service, the edited content has some formats and conventions. Enter crontab-u root-e.
In the vi editing mode, the edited content must conform to the following format:
*/1 ***** ls>/tmp/ls.txt
Edit the/etc/crontab file and add a line at the end: 30 5 * root init 6. Then, the system is configured to automatically restart at 05:30 every morning.
Rows in the crontab file are composed of six fields. Different fields are separated by spaces or the tab key. The first five fields specify the time when the command will be run.
Minutes (0-59)
Hour (0-23)
Date (1-31)
Month (1-12)
Day of the week (0-6, 0 indicates Sunday, and 7 indicates Sunday)
The 6th fields are strings to be executed at the appropriate time.
Example:
# MIN HOUR DAY MONTH DAYOFWEEK COMMAND
#06:10 every morning
10 6 **** date
# Every two hours
0 */2 * date
# Every two hours from PM to am, am
0 23-7/2, 8 **** date
# Am on the 4th day of each month and from Monday to Wednesday
0 11 4*1-3 date
# Am, January 1, January 1
0 4 1 1 * date
Note: When Using crontab, the environment variables that can be accessed in the running script may not be consistent with those in the current test environment, it is safer to set the environment variable (export) in the running script program)
(1ndcreate a file named crond.txt as follows and restart it at 05:36 every morning.
36 5 * reboot
(2) upload to the/opt directory
(3) run the command
Crontab/opt/crond.txt
Crontab-l
Make the configuration file take effect: If the configuration file takes effect, you have to restart cron. Remember, since the cron configuration file under each user is modified, You have to restart the cron server.
In Fedora and RedHat, we should use:
/Etc/rc. d/crond restart
If you want crond to run at startup, you should change its running level:
Chkconfig-level 35 crond on
Service crond status: Check the cron service status. If it is not started, service crond start starts it. The cron service is a scheduled service. You can use the crontab command to add or edit tasks that require scheduled execution.
Use crontab in Linux to create scheduled tasks
Routine scheduling of crontab in Linux
Linux crontab does not run troubleshooting
Ubuntu uses crontab for scheduled tasks
Linux scheduled task (at batch crontab anacron)
This article permanently updates the link address: