使用Cron运行Python脚本? [英] Running a Python Script using Cron?

查看:218
本文介绍了使用Cron运行Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python脚本,我想添加到cron。

I have a python script that I'd like to add to cron.

脚本有+ x权限。

如何将它添加到crontab? (例如,我希望它每分钟运行)。

How shall I add it to crontab? (say, I want it to run every minute).

重要提示:当我导航(使用shell)到脚本的文件夹,我不能使用./ script_name.py;它不工作。但是,当我使用Python script_name.py运行它时,一切正常。

Important: when I navigate (using the shell) to the script's folder, I cannot run it using "./script_name.py"; it doesn't work. Yet, when I run it using "Python script_name.py", everything works.

推荐答案

,您应该以 script_name.py 的身份运行脚本,并且您的脚本符合以下条件:

From cron you should be running the script as script_name.py and your script meets the following criteria:


  • 已设置可执行位

  • 脚本的hash-bang已正确设置,例如。 #!/ usr / bin / env python

  • 可从 PATH

    • 例如将它放在 / usr / local / bin / / opt / local / bin / 您的系统 PATH 。)

    • Executable bit is set
    • The script's hash-bang is set correctly eg. #!/usr/bin/env python
    • it is accessible from the PATH
      • e.g. place it in /usr/local/bin/ or /opt/local/bin/ (and they are accessible to your system PATH.)

      如果满足这些条件,您应该可以从本地系统上的任何位置运行它 script_name.py

      If these conditions are met, you should be able to run it from anywhere on your local system as script_name.py

      这篇关于使用Cron运行Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆