Two ways to create timed tasks under ubuntu and solutions to common problems

Keywords: Linux crontab Asterisk sudo vim

The goal of creating a timed task is to get rid of human-made repetitive running of the program.

0. First check whether you install crontab with the following instructions.



crontab -l
If it does, the following instructions appear
LC_CTYPE="zh_CN.utf-8"
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

If not, install it as follows



apt-get install cron
There are two ways:

1. Crontab-e Directive
Input crontab-e directly into the terminal and enter the editing interface of the nano editor. It is more friendly and simple than vi and vim editors.
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
  7 15 * * * /usr/bin/python2.7 /home/mabo/Desktop/camera.py
It's important to note that if you want it to be executed in front of a line, you can't have #.
M h DOM mon Dow command is the abbreviation of minute; hour; day of month; month; day of week, respectively.

minute: Represents minutes, which can be any integer between 0 and 59.

Hour: Represents an hour, which can be any integer between 0 and 23.

day: Represents a date, which can be any integer between 1 and 31.

Month: Represents a month, which can be any integer between 1 and 12.

Week: It means the day of the week. It can be any integer between 0 and 7, where 0 or 7 represents Sunday.

Command: The command to be executed can be either a system command or a script file written by oneself

[Note]: It's important to note that before writing command instructions, you need to test whether the command can be executed at the terminal. I'll talk about it later.

 

Asterisk (*): Represents all possible values, for example, if the month field is an asterisk, it means that the command operation is executed monthly after meeting the constraints of other fields.

Comma (,): You can specify a list range with comma-separated values, such as "1, 2, 5, 7, 8, 9".

Bar (-): A range of integers can be represented by bars between integers, such as "2-6" for "2,3,4,5,6".

Positive and oblique (/): The interval frequency of the time specified by the positive and oblique lines, such as "0-23/2", can be used to indicate that it is executed every two hours. At the same time, the forward and slash lines can be used with asterisks, such as */10, if used in the minute field, indicating that they are executed every ten minutes.

 

2. Open / etc/crontab with an editor

Choose one or two.
sudo nano /etc/crontab
sudo vi /etc/crontab
# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
#
17 16   * * *   mabo    /usr/bin/python2.7 /home/mabo/Desktop/camera.py

After opening it, as shown in the figure above, the bottom line is my own timed task. One difference from the first approach is the user level problem.

 

Common problem:

Q: Does the scheduled task not execute?

A: In this case, you should first check whether the program has any problems. You may think that there is no problem running in pycharm, but now you are using IDE, so you need to copy all the command instructions mentioned above to the terminal to see if the operation is successful. If not, you can solve the problem according to the prompt until it runs. No problem.

The above method has been able to solve a large part of the problem, after eliminating errors in the program, if you use the second method, then you can modify root for your general user, and re-execute.

 

Q: Commonly used instructions for timing tasks?

A:

crontab -l              //View all the timed tasks set
service crond start //Start up service service crond stop //Shut down services service crond restart //Restart service

 

Q: How do I check the performance of my scheduled tasks?

A:

Choose one of the following.
sudo nano /var/log/cron.log
sudo vi /var/log/cron.log

In the log, you can see how the timed tasks you set up work. After many times of practice, it is found that when running normally, only the same instructions as your command instructions will appear in the log, and other redundant instructions will lead to the failure of the timing task. Eliminate errors according to these superfluous instructions















Posted by cesar_ser on Thu, 22 Aug 2019 01:44:51 -0700