grammar:
crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [UserName]|File ]
illustrate :
Crontab is used to allow users to execute programs at fixed times or fixed intervals. In other words, it is similar to the user's schedule. -u user refers to setting the schedule of the specified user. The premise is that you must have its permissions (for example, root) to specify other people's schedules. If -u user is not used, it means setting your own schedule.
parameter :
-e [UserName]: Execute a text editor to set the schedule. The default text editor is VI. If you want to use another text editor, please set the VISUAL environment variable to specify which text editor to use (for example, setenv VISUAL joe)
-r [UserName]: Delete the current schedule
-l [UserName]: List the current schedule
-v [UserName]: List the status of user cron jobs
The format of the schedule is as follows :
f1 f2 f3 f4 f5 program
where f1 represents minutes, f2 represents hours, f3 represents the day of a month, f4 represents the month, and f5 represents the day of the week. program represents the program to be executed.
When f1 is *, it means the program will be executed every minute, when f2 is *, it means the program will be executed every hour, and so on.
When f1 is a-b, it means that it will be executed from minute a to minute b. When f2 is a-b, it means that it will be executed from hour a to hour b, and so on.
When f1 is */n, it means that it will be executed every n minutes. When f2 is */n, it means it will be executed every n hours. And so on.
When f1 is a, b, c,..., it means that it will be executed in the a, b, c,... minute. When f2 is a, b, c,..., it means that it will be executed in the a, b, c... hour, and so on.
Users can also store all settings in a file first and use crontab file to set the schedule.
Because the Unix versions are different, some syntaxes are different. For example, if you use */n to set interval execution in HP Unix Aix, a syntax error will occur. In this type of Unix, interval execution can only be in enumeration mode. Please see the example for details.
How to use:
crontab –e
After adding:
*/1 * * * * /usr/local/curl/bin/curl
Executed every minute.
Then /sbin/service crond restart to restart
Detailed method:
Use VI to edit a cronfile file, and then enter a well-formatted schedule in this file. After editing is complete, save and exit.
Enter at the command line
$: crontab cronfile
In this way, the cronfile file is submitted to the cron process. At the same time, a copy of the newly created cronfile has been placed in the / v a r / sp o o l / cron directory, and the file name is the user name.
Execute tasks with a certain frequency
Linux starts the crond process by default, and the crond process does not require users to start or shut down.
The crond process is responsible for reading and executing scheduled tasks. Users only need to write the corresponding scheduling script into the cron scheduling configuration file.
The cron scheduling files include the following:
- crontab
- cron.d
- cron.daily
- cron.hourly
- cron.monthly
- cron.weekly
If the task used is not executed in hourly monthly weekly mode, the corresponding crontab can be written to the crontab or cron.d directory.
Example explanation one:
Execute the script /opt/bin/test-cron.sh every minute
You can create a new script echo-date.sh in cron.d
The content is
*/1 * * * * root /opt/bin/test-cron.sh
You can also use the at command to control running tasks at a specified time
at -f test-cron.sh -v 10:25
Where -f specifies the script file, -v specifies the running time
Example explanation two:
System cron settings: /etc/crontab
Through the /etc/crontab file, you can set tasks to be executed regularly by the system. Of course, if you want to edit this file, you must have root permissions.
0 7 * * * root mpg123 ~/wakeup.mp3 minutes hour day month week
Example:
0 4 ***
Suppose I want to execute a certain command every 2 minutes, or I want to execute a command at 6 o'clock, 12 o'clock, or 18 o'clock every day. Periods like this can be set by "/" and ",":
*/2 * * * * root ............. #Execute every two minutes... 0 6,12,18 * * * root ............. #Execute every day at 6:00, 12:00, and 18:00...
every two hours
0 */2 * * * echo 'have a break now.' >> /tmp/test.txt
Every two hours between 11pm and 8am, 8am
0 23-7/2,8 * * * echo 'have a good dream:)' >> /tmp/test.txt
On the 4th of every month and every Monday to Wednesday at 11 a.m.
0 11 4 * 1-3 command line
January 1, 4 a.m.
0 4 1 1 * command line
cron method to execute PHP
00 00 * * * usr/local/php/bin/php -q /path/to/testing.php > /dev/null 2>&2
This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)". Please keep the following tags for sharing and interpretation:
Original author:Jake Tao,source:"Linux crontab command to let the server automatically execute PHP"