Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Ubuntu regularly executes python script instance code

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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

Preface

This article will introduce how to execute shell script and python script regularly under ubuntu system. Ubuntu system has a manager crontab for scheduled tasks. We only need to edit scheduled tasks and restart the timed task service.

Crontab

Edit scheduled tasks

Crontab-e

Parameter definition:

-u specify user,-l list user task schedule,-r delete user task,-e edit user task

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 a.m every week with:

# 0 5 * * 1 tar-zcf / var/backups/home.tgz / home/

Chinese explanation:

Format

M h dom mon dow command

The above are abbreviations. Full spelling comparison is provided here:

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

The meaning is as follows:

M the minute of each hour to execute the task h the hour of the day to execute the task dom the day of the month to execute the task mon the month of the year to execute the task dow the day of the week to execute the task-the program specified by command

Order for hours, days, months and weeks

0-59 0-23 1-31 1-120-6 command

Other:

Where 0 in the middle of the week means Sunday. * represents any time, such as the first minute, using * means every minute of every hour is executed-to indicate the interval, such as 1-3. If the interval is not continuous, it can be used, for example, wq save exit after editing.

Restart the service

Service cron restart

Matters needing attention

Note that the absolute path must be used. Otherwise, the execution may fail.

For example, we have to implement

Python bwh.py

So the first thing you need to do is

Which python

To see the real path of the python command

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

Then, check the full path of bwh.py, under 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 refers to the execution of / root/.pyenv/shims/python / app/python/blog/bwh.py at 9: 00 sharp every day and the output of the printed log to / tmp/new_blog_bwh.log

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report