ubuntu定时运行python_ubuntu定时执行python脚本实例代码

原文链接:https://vien.tech/article/157

前言

本文将介绍ubuntu系统下如何定时执行shell脚本、python脚本,ubuntu系统有一个定时任务的管理器crontab,我们只需要编辑定时任务,然后重启定时任务服务就好了。

crontab

编辑定时任务

crontab -e

参数定义:

-u 指定用户, - -l 列出用户任务计划,

-r 删除用户任务,

-e 编辑用户任务

英文介绍:

# 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/

中文解释:

格式

m h dom mon dow command

以上为缩写,这里提供全拼对照:

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

含义如下:

m 每个小时的第几分钟执行该任务

h 每天的第几个小时执行该任务

dom 每月的第几天执行该任务

mon 每年的第几个月执行该任务

dow 每周的第几天执行该任务 - command 指定要执行的程序

分 小时 日 月 星期 命令

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

其他:

其中星期中0表示周日。

* 代表任何时间,比如第一个分钟,用 * 就代表每一小时的每一分钟都执行

- 表示区间,比如1-3

, 如果区间不连续,可以用,例如1,3,6    编辑完成后wq 保存退出

重启服务

service cron restart

注意事项

注意,一定要用绝对路径。否则可能会执行失败。

比如,我们要执行

python bwh.py

那么你需要干的第一件事是

which python

以此来查看python命令的真正路径

root@ubuntu:~# which python

/root/.pyenv/shims/python

然后,查看bwh.py的全路径,在bwh.py所在文件夹下

pwd

/app/python/blog

然后路径便为

/app/python/blog/bwh.py

所以整条记录应该这样编辑

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

上面的记录是指每天9点整执行/root/.pyenv/shims/python /app/python/blog/bwh.py并将打印日志输出到/tmp/new_blog_bwh.log

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对聚米学院的支持。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Python的云计算例子有很多,以下是其中的两个例子: 1.使用Python和OpenStack API进行云资源管理 OpenStack是一个开源的云计算平台,它提供了一系列API,可以用Python编写脚本来管理云资源。例如,可以使用Python脚本创建虚拟机、存储卷、网络等资源,也可以使用Python脚本监控云资源的使用情况。 以下是一个使用Python和OpenStack API创建虚拟机的例子: ```python import openstack # 创建连接 conn = openstack.connect(cloud='mycloud') # 创建虚拟机 server = conn.create_server( name='myserver', image='ubuntu', flavor='m1.small', network='private', key_name='mykey', security_groups=['default'] ) # 等待虚拟机创建完成 conn.wait_for_server(server) # 获取虚拟机IP地址 ip_address = conn.get_server_ip(server) print('Server created with IP address:', ip_address) ``` 2.使用Python和AWS SDK进行云资源管理 AWS是一个云计算平台,它提供了一系列SDK,可以用Python编写脚本来管理云资源。例如,可以使用Python脚本创建EC2实例、S3存储桶、RDS数据库等资源,也可以使用Python脚本监控云资源的使用情况。 以下是一个使用Python和AWS SDK创建EC2实例的例子: ```python import boto3 # 创建连接 ec2 = boto3.resource('ec2') # 创建EC2实例 instance = ec2.create_instances( ImageId='ami-0c55b159cbfafe1f0', MinCount=1, MaxCount=1, InstanceType='t2.micro', KeyName='mykey', SecurityGroups=['default'] )[0] # 等待EC2实例创建完成 instance.wait_until_running() # 获取EC2实例IP地址 ip_address = instance.public_ip_address print('EC2 instance created with IP address:', ip_address) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值