Ubuntu regularly executes Python script example code

Ubuntu regularly executes Python script example code

Original link: https://vien.tech/article/157

Preface

This article will introduce how to schedule the execution of shell scripts and python scripts under Ubuntu system. Ubuntu system has a scheduled task manager crontab. We only need to edit the scheduled task and then restart the scheduled task service.

crontab

Editing scheduled tasks

crontab -e

Parameter definition:

  • -u specifies the user, - -l lists the user's task schedule,
  • -r delete user tasks,
  • -e Edit user tasks

English introduction:

# 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 am every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

Chinese explanation:

Format

mh dom monitor command

The above are abbreviations, here is the full spelling comparison:

minute (m), hour (h), day of month (dom), month (mon), day of week (dow)

The meaning is as follows:

  • m The minute of every hour that the task is executed
  • h The hour of the day when the task is executed
  • dom The day of the month when the task is executed
  • mon The number of months of each year to execute this task
  • dow executes the task on which day of the week - command specifies the program to be executed

Minute Hour Day Month Day of Week Command
0-59 0-23 1-31 1-12 0-6 command

other:

  • The weekday 0 represents Sunday.
  • * represents any time, such as the first minute, using * means that it will be executed every minute of every hour
  • - indicates an interval, such as 1-3
  • , If the interval is not continuous, you can use, for example, 1,3,6 After editing, press wq to save and exit

Restart the service

service cron restart

Precautions

Note that you must use an absolute path. Otherwise the execution may fail.

For example, we want to execute

python bwh.py

So the first thing you need to do is

which python

This is to view the real path of the python command

root@ubuntu:~# which python
/root/.pyenv/shims/python

Then, check the full path of bwh.py in the folder where bwh.py is located.

pwd
/app/python/blog

Then the path is

/app/python/blog/bwh.py

So the whole record should be edited like this

0 9 * * * /root/.pyenv/shims/python /app/python/blog/bwh.py > /tmp/new_blog_bwh.log

The above record means that /root/.pyenv/shims/python /app/python/blog/bwh.py is executed at 9 o'clock every day and the print log is output to /tmp/new_blog_bwh.log

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Python script background execution method
  • How to set up scheduled tasks in Windows 10 to automatically execute Python scripts
  • Example of a python script executing a CMD command and returning the result
  • Several ways to pause Python script execution (summary)
  • Detailed explanation of Python executing shell scripts to create users and related operations
  • Python uses file lock singleton to execute script
  • Solve the problem that the imported package cannot be found when executing the script on the Python command line
  • Solve the problem of not being able to execute python scripts after installing pycharm
  • Summary of three execution methods of Python scripts

<<:  How to make JavaScript sleep or wait

>>:  MySQL slow log online problems and optimization solutions

Recommend

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

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

3 simple ways to achieve carousel effects with JS

This article shares 3 methods to achieve the spec...

How to install Nginx in Docker

Install Nginx on Docker Nginx is a high-performan...

Windows Server 2016 Quick Start Guide to Deploy Remote Desktop Services

Now 2016 server supports multi-site https service...

Mysql multi-condition query statement with And keyword

MySQL multi-condition query with AND keyword. In ...

VMware kali virtual machine environment configuration method

1|0 Compile the kernel (1) Run the uname -r comma...

Solution to the conflict between nginx and backend port

question: When developing the Alice management sy...

zabbix custom monitoring nginx status implementation process

Table of contents Zabbix custom monitoring nginx ...

WeChat applet implements simple chat room

This article shares the specific code of the WeCh...

How to configure multiple tomcats with Nginx load balancing under Linux

The methods of installing nginx and multiple tomc...

Detailed explanation of this reference in React

Table of contents cause: go through: 1. Construct...

The presentation and opening method of hyperlink a

<br />Related articles: How to prompt and op...

Network management and network isolation implementation of Docker containers

1. Docker network management 1. Docker container ...

Detailed explanation of various join summaries of SQL

SQL Left Join, Right Join, Inner Join, and Natura...