Ubuntu crontab timed Python

Source: Internet
Author: User
Tags python script

Tag: Log txt Color success space timed to execute test ACK span

Ubuntu system, timed to execute Python script.

Purpose: Perform timer_test.py once per minute

timer_test.py Path/home/li/d/pythonwork/test/timer_test.py

1 # Coding:utf-8 2 Import  Time 3 t = time.strftime ('%y-%m-%d%h:%m:%s', Time.localtime (Time.time ()))  4' execution time:'\ n'5 Print str

Start Set timing

1 opening the terminal on the desktop

2 Input: sudo crontab-e

Then enter: */1 * * * * python/home/li/d/pythonwork/test/timer_test.py >>/home/li/d/pythonwork/test/testcrontab.log 2 >&1

or enter the absolute path of Python: */1 * * * *, usr/bin/python2.7/home/li/d/pythonwork/test/timer_test.py >>/home/li/d/pythonwor K/test/testcrontab.log 2>&1

Press CTRL o Save → If prompted, press ENTER directly →ctrl X to leave

As follows

3 input: sudo crontab-l to see your edited files.

4 This is the go to/home/li/d/pythonwork/test/path below you can see the Testcrontab.log file, open as follows:

The timing is set successfully.

----------------------------------------------------------------------------

Other:

In Testcrontab.log, the latter 2>&1 means outputting the wrong output to the standard output (2 means the error,2> means the error output,& equals, 1 is correct), Therefore, if you run an error, you will output the error to the previously defined log.

Here's a little explanation of the meaning of each line in crontab. Each row in crontab represents a task that is performed on a regular basis and is divided into 6 parts. The first 5 sections indicate when to execute the command, and the last part represents the command to execute. Each part is separated by a space, except that the last part (the command) can use a space inside it, and no space in the other part. The first 5 sections represent: minutes, hours, days, months, and weeks, with the following values for each part:

Minutes 0-59

Hours 0-23

Day 1-31

Month 1-12

Week 0-6 0 indicates Sunday

In addition to these fixed values, you can also match the asterisk (*), comma (,), and slash (/) to indicate some other meanings:

Asterisks indicate any value, such as filling in the hour section * for any hour (per hour)

Commas can allow multiple values to be filled in a section, such as filling in minutes with 1,3 for one minute or three minutes

The slash general mate * is used, representing how often, for example in the hour section fill in the */2 representative every two minutes. So there's no difference between */1 and *

*/2 can be considered as any value that can be divisible by 2.

    * * * * *                  #  perform a task    every other minute    0        *                  * * * #  0 points per hour to perform a task, such as    6:00,10:00    6,10     *    2 *    *                  #  2nd every month, 6 minutes and 10 minutes    per hour.    */3,*/5 * * * * *                  #  Perform tasks every 3 minutes or 5 minutes, such as 10:03,10:05,10:06  
0 7 * * */BIN/LS#/bin/ls every morning at 7.0 6-12/3 */usr/bin/backup#during the December, every 3 hours in the morning from 6 to 12./usr/bin/backup0 * * 1-5 mail-s"Hi"[Email protected] </tmp/maildata#Monday to Friday every 5:00 send a letter to [email protected]0-23/2 * * * echo"haha">>/tmp/haha.txt#every day of the month 0:20 midnight, 2:20, 4:20 .... Write haha to the/tmp/haha.txt file

Reference:

How to use Ubuntu under Crontab commands

Use of Scheduled Tasks under Ubuntu (crontab)

Ubuntu crontab timed Python

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.