I am using ubuntu16, so everything is OK in Ubuntu, and it should be similar in other Linux systems.
1 Scheduled Tasks, crontab command options:
-u Specifies a user,
-l lists a user's task schedule,
-R Deletes a user's task,
-e Edit a user's tasks
2 cron file Syntax:
Hour of the week order
0-59 0-23 1-31 1-12 0-6 Command (value range, 0 for Sunday One a row corresponds to a task)
The CRONTAB-E command can be used to edit, edit the corresponding user's cron file under/var/spool/cron, or modify the/etc/crontab file directly.
The specific format is as follows:
Minute Hour Day Month Dayofweek command
Minutes hours days months days per week order
Each field represents the following meanings:
Minute the first few minutes of every hour to perform the task
Hour the first few hours of the day to perform this task
Day of the month to perform the task
Month months of the year to perform this task
DayOfWeek the day of the week to perform the task
Command Specifies the program to execute
Remember the meanings of several special symbols:
"*" represents a number in the range of values,
"/" stands for "every",
"-" represents a number to a number,
"," separate a few discrete numbers
3 Add a new scheduled task
Crontab-e then add the corresponding task, Wq save the disk to exit.
4 Viewing Scheduled Tasks
View scheduling Tasks
Crontab-l//List all current Scheduled tasks
Crontab-l-u JP//List all scheduling tasks for user JP
5 Example 1, adding a scheduled task
* * * * * date > Test
6 Restart Cron Service
Service Cron Restart
Note: You will need to restart the service each time you finish modifying crontab.
You can see that the test file is updated every second.
Example 2, write a Python script, execute it regularly
The test2.py file code is as follows
#!/usr/bin/Pythonimport timedef fun1 (): Lastsec =4With open ("Test",‘a+ ") as fobj: for i in Range ( 2*lastsec): TM = Time.strftime ( "%y-%m-%d%h:%m:%s" ) fobj.write (Tm+ "\n" ) time.sleep (0.5 ) def test (): Fun1 () if __name__== "__main__" : Test ()
To execute the file, you need to add executable permissions
CHOMD +x test2.py
Add a new Scheduled task
*/2 * * * */usr/bin/python/home/pc/work/env/project/test2.py
Note: For security reasons, all paths need to be absolute paths.
But did not execute, and later on the Internet to find information, need to check the log, looking for a half-day no, the original Ubuntu system does not open the default log. So to open the log first, log files in/var/log/cron.log
Method:
1) Modify the Rsyslog file to delete the # before the #cron.* in the/etc/rsyslog.d/50-default.conf file;
2) Restart the Rsyslog Services service Rsyslog restart
3) Restart Cron Services service cron restart
This will allow you to see the log.
August 15:42:01 pc-virtual-machine cron[10196]: Pam_unix (cron:session): Session opened for user PC by (uid=0)
August 15:42:01 pc-virtual-machine cron[10197]: (PC) CMD (/usr/bin/python/home/pc/work/env/project/test2.py)
You can see that the scheduled task is executed. But why is there no output file test?
Then manually execute the commands in the scheduled task yourself,/usr/bin/python/home/pc/work/env/project/test2.py
[Email protected]:/var/spool$/usr/bin/python/home/pc/work/env/project/test2.py
Traceback (most recent):
File "/home/pc/work/env/project/test2.py", line +, in <module>
Test ()
File "/home/pc/work/env/project/test2.py", line +, in test
FUN1 ()
File "/home/pc/work/env/project/test2.py", line 9, in FUN1
With open (' Test ', ' A + ') as Fobj:
IOError: [Errno] Permission denied: ' Test '
Give me an error, look carefully, the original script has a problem, the output test file should be an absolute path, should not be a relative path, it is possible that the program is not in the folder where the script force execution, so there may be a permissions issue.
So, after modification, the test2.py file is as follows
#!/usr/bin/Pythonimport timeimport osdef fun1 (): Lastsec =4CurDir =OS.GETCWD () filename = Os.path.join (CurDir,"Test") with open (‘/home/pc/work/env/project/test‘,‘a+ ") as fobj: for i in Range ( 2*lastsec): TM = Time.strftime ( "%y-%m-%d%h:%m:%s" ) fobj.write (Tm+ "\n" ) time.sleep (0.5 ) def test (): Fun1 () if __name__== "__main__" : Test ()
Then restart the Cron service.
Ls
startpy.sh Test test2.py
You can see that the output is available.
More Test
2016-08-27 16:52:01
2016-08-27 16:52:02
2016-08-27 16:52:02
2016-08-27 16:52:03
2016-08-27 16:52:03
2016-08-27 16:52:04
2016-08-27 16:52:04
2016-08-27 16:52:05
Done!!!
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
Note: If the file operation is involved in the script file, use the absolute path, which is where I fell in the pit.
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
Under normal circumstances should be able to run, if there is a problem, you can follow the following steps to find the problem
View cron log, in/var/log/cron.log, Ubuntu is not enabled by default, so you have to manually open the log
If there is a log output, indicating that the scheduled task is running properly, it is possible that you have a problem with the configuration, you manually run the cmd command in the scheduled task, as I started with the script itself is a problem (do not use the relative directory of the file), if successful, it indicates that the scripting environment variables are problematic, For specific reasons, you will need to check the error message sent by crontab.
Scheduled task execution script in Linux crontab-compact version