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

IIS7 IIS8 http automatically jumps to HTTPS (port 80 jumps to port 443)

IIS7 needs to confirm whether the "URL REWRI...

MySQL count detailed explanation and function example code

Detailed explanation of mysql count The count fun...

Vue uses element-ui to implement menu navigation

This article shares the specific code of Vue usin...

Example code for implementing dotted border scrolling effect with CSS

We often see a cool effect where the mouse hovers...

Vue implements QR code scanning function (with style)

need: Use vue to realize QR code scanning; Plugin...

Tudou.com front-end overview

1. Division of labor and process <br />At T...

How to use MySQL binlog to restore accidentally deleted databases

Table of contents 1 View the current database con...

How to implement vue page jump

1. this.$router.push() 1. Vue <template> &l...

Introduction to HTML Chinese Character Encoding Standard

In HTML, you need to specify the encoding used by...

JavaScript to achieve floor effect

This article shares the specific code of JavaScri...

Deploy Varnish cache proxy server based on Centos7

1. Varnish Overview 1. Introduction to Varnish Va...

Docker private warehouse harbor construction process

1. Preparation 1.1 harbor download harbor downloa...

Using js to implement the two-way binding function of data in Vue2.0

Object.defineProperty Understanding grammar: Obje...