Get the App
SLTechnology News&Howtos  ›  Servers  › 

Example Analysis of self-starting and scheduled tasks of Python script under Linux

Shulou Source: shulou.com Published: 2022-06-01 17:54:08 11月14日 Update

This article mainly shows you "Python script self-start and timed task example analysis under Linux", the content is simple and easy to understand, organized clearly, I hope to help you solve doubts, let Xiaobian lead you to study and learn "Python script self-start and timed task example analysis under Linux" this article bar.

First, let Python run automatically with Linux boot

Prepare the script auto.py to start

Edit the following files with root privileges

sudo vim /ect/rc.local

Edit the command for the startup script above exit 0

/usr/bin/python3.5 /home/edgar/auto.py > /home/edgar/auto.log

Finally restart Linux and the script will run automatically and print logs.

Second, let Python script start regularly

Prepare the timed startup script auto.py

Edit the following files with root privileges

sudo vim /etc/crontab

Add the following command at the end of the file

2 * * * * root /usr/bin/python3.5 /home/edgar/auto.py > /home/edgar/auto.log

The above code means to execute the script and print the log every two minutes.

III. crontab preparation explanation

basic format

* * * * * user command Time sharing day month week user command

IV. Examples

1. Perform once every minute

* * * * * user command

2. Perform once every 2 hours

* */2 * * * user command (/indicates frequency)

3. Perform once a day at 8:30

30 8 * * * user command

4. Once every 30 and 50 minutes

30,50 * * * user command (, indicates juxtaposition)

4. Perform once at 8:30 from the 3rd to the 6th of each month

30 8 3-6 * * user command (-indicates range)

5. Perform once every Monday at 8:30

30 8 * * 1 user command (week range 0- 7, 0 and 7 represent Sunday) The above is "Python script self-startup and timed task example analysis under Linux" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Scripts tasks examples analysis content commands files articles hours logs permissions scope preparation learning help running code representative meaning easy to understand Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux MariaDB Redmi Huawei