Detailed steps for Python script self-start and scheduled start under Linux

Detailed steps for Python script self-start and scheduled start under Linux

1. Python automatically runs at startup

Suppose the Python self-starting script is auto.py Then edit the following files with root privileges:

sudo vim /etc/rc.local

If you don't have rc.local please read this article

Edit the command to start the script above exit 0

/usr/bin/python3 /home/selfcs/auto.py > /home/selfcs/auto.log

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

2. Start the Python script regularly

Edit the following file with root privileges

sudo vim /etc/crontab 

Add the following command at the end of the file

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

The above code means that the script will be executed every two minutes and the log will be printed.

3. Crontab Writing Explanation

Basic format

* * * * * user command
Time-sharing, daily, monthly and weekly user commands

3.1 Example 1: Execute once every minute

* * * * * user command

2. Execute every 2 hours

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

3. Execute once every day at 8:30

30 8 * * * user command

4. Execute once at 30 and 50 minutes of every hour

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

5. Execute once at 8:30 on the 3rd to 6th of each month

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

6. Execute once every Monday at 8:30

30 8 * * 1 user command (the range of the weekday is 0-7, 0 and 7 represent Sunday)

Summarize

The above is the detailed steps for the self-start and scheduled startup of Python scripts under Linux introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Detailed method of packaging Python3 program under Windows and Linux
  • Python modifies the permissions of files (folders) in Linux
  • Detailed tutorial on installing Python 3.8.1 on Linux
  • Tutorial on upgrading and installing python 3.8 and configuring pip and yum under Linux
  • Two ways to install Python3 on Linux servers
  • Python logs in to Linux through SSH and implements operations
  • Detailed explanation of the process of deleting the built-in version of Python in Linux

<<:  How to use a field in one table to update a field in another table in MySQL

>>:  JS implements a simple brick-breaking pinball game

Recommend

Centos8 (minimum installation) tutorial on how to install Python3.8+pip

After minimizing the installation of Python8, I i...

Docker cleaning killer/Docker overlay file takes up too much disk space

[Looking at all the migration files on the Intern...

CSS3 sample code to achieve element arc motion

How to use CSS to control the arc movement of ele...

mysql method to recursively search for all child nodes of a menu node

background There is a requirement in the project ...

5 Ways to Send Emails in Linux Command Line (Recommended)

When you need to create an email in a shell scrip...

The docker-maven-plugin plugin cannot pull the corresponding jar package

When using the docker-maven-plugin plug-in, Maven...

How to check if a table exists in MySQL and then delete it in batches

1. I searched for a long time on the Internet but...

Introduction to the use of common Dockerfile commands

Table of contents 01 CMD 02 ENTRYPOINT 03 WORKDIR...

Detailed explanation of Nginx log customization and enabling log buffer

Preface If you want to count the source of websit...

Solution to mysql error code 1064

If the words in the sql statement conflict with t...

Analysis of MySql index usage strategy

MySql Index Index advantages 1. You can ensure th...

Node.js file copying, folder creation and other related operations

NodeJS copies the files: Generally, the copy oper...

How to write the introduction content of the About page of the website

All websites, whether official, e-commerce, socia...