• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    ubuntu定时执行python脚本实例代码

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

    前言

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

    crontab

    编辑定时任务

    crontab -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)

    含义如下:

    分      小时    日      月       星期     命令
    0-59   0-23   1-31   1-12     0-6     command

    其他:

    重启服务

    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

    总结

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

    上一篇:Linux下使用killall命令终止进程的8大用法实例详解
    下一篇:Linux redis-Sentinel配置详解
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

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