Ubuntu uses Crontab to start timed tasks

Source: Internet
Author: User

Ubuntu Cron performs tasks regularly

80545004

Refer to the above two blog posts, through the practice of completing their own scheduled tasks

crontab Command

The

crontab command is used to install, delete, or list tables used to drive a cron background process. That is, the user places the sequence of commands that need to be executed in the crontab file for execution, and each user can have his or her own crontab file. Here are some parameters and instructions for this command:
1) crontab-u/* Sets a user's Cron service */ ,
2) crontab-l/* Lists details of a user's cron service */ 
3) crontab- R/* Delete a user's cron service */ 
4) crontab-e/* Edit a user's cron service */ 
Parameter name meaning example
-L displays the contents of the user's crontab file crontabl–l
I give prompt Crontabl-ri
-R to remove user's crontab file from crontab directory crontabl-r
-e Edit user's crontab file crontabl-e
crontab file The/etc/crontab file syntax is as follows:
Minute Hour Day Month Dayofweek command
minute hours days month days a week command
the meaning and range of values represented by each field are as follows:
Minute : minutes (0-59), which represents the first few minutes of each hour to perform the task
Hour: Hour (1-23), which represents the hours of the day when the task is performed
Days: Date (1-31), which represents the day ordinal of a month to perform the task
Month: month (1-12), table The first month of the year performs the task
DayOfWeek: Week (0-6, 0 for Sunday), which represents the day of the week to perform the task
command: Specify the command to execute (if there are too many commands to execute, you can write these commands into a script, And then call this script directly here, remember to write the full path of the command when you call
in these fields, except that "command" is the field that must be specified each time, the other fields are optional fields, depending on the need to decide. For a field that is not specified, "*" is used to fill its position. At the same time, Cron supports writing similar to regular expressions and supports several special symbolic definitions:

"*", representing all numbers within the range of values;
"/", on behalf of "each" ("*/5", representing every 5 units);
"-", represented from a number to a number ("1-4", representing 1-4 units);
",", separating several discrete numbers;
Examples are as follows:

5 * * * * ls//Specify the first 5 minutes per hour to execute the LS command */
5 * * * ls//specify 5:30 to execute LS command per day */
7 8 * * ls//Specify 7:30 points per month for 8th # Execute LS command */
7 * * * Root run-parts/etc/cron.daily//daily 7:50 executes all executables in the/etc/cron.daily directory as root */

New Cron Task

1. Execute the following command to add a task

# CRONTAB-E

2. Restart the cron service

# Service Cron Restart

Summary:

Linux takes a script file as a summary of a scheduled task, as an example of a Python script:

1 Create script file test.py, add the following line to the beginning of the file

#!/usr/bin/python

The purpose of the above line is to illustrate the use of that interpreter to execute the file, and if you don't know where the Python interpreter is, you can use the command which Python to see

2 Add executable permissions to the file

chmod +x test.py

3 Adding a scheduled task

Crontab-e

Append a line to the file, */2 * * * */usr/bin/python/home/pc/work/env/project/test.py

Save exit,: Wq

4 Restart Cron Service

Service Cron Restart

End

Execute error

[Email protected]:/var/log$ service cron restart= = = = Authenticating for Org.freedesktop.systemd1.manage-units == = ='cron.service'. Authenticating As:ubuntu (Ubuntu) password:polkit-agent-helper-1: Pam_authenticate failed:authentication Failure= = = AuthenticationFAILED = = = =Systemctl status Cron.service '  for details.
View Code

Insufficient permissions

sudo service cron restart

Ubuntu uses Crontab to start timed tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.