简体   繁体   English

使用Cron运行Python脚本?

[英]Running a Python Script using Cron?

I have a python script that I'd like to add to cron. 我有一个python脚本,我想添加到cron。

The script has +x permission on it. 该脚本具有+ x权限。

How shall I add it to crontab? 我该如何将它添加到crontab? (say, I want it to run every minute). (比方说,我希望它每分钟运行一次)。

Important: when I navigate (using the shell) to the script's folder, I cannot run it using "./script_name.py"; 重要提示:当我(使用shell)导航到脚本的文件夹时,我无法使用“./script_name.py”运行它; it doesn't work. 它不起作用。 Yet, when I run it using "Python script_name.py", everything works. 然而,当我使用“Python script_name.py”运行它时,一切正常。

From cron you should be running the script as script_name.py and your script meets the following criteria: cron您应该将脚本作为script_name.py运行,并且您的脚本符合以下条件:

  • Executable bit is set 可执行位已设置
  • The script's hash-bang is set correctly eg. 脚本的hash-bang设置正确,例如。 #!/usr/bin/env python
  • it is accessible from the PATH 它可以从PATH访问
    • eg place it in /usr/local/bin/ or /opt/local/bin/ (and they are accessible to your system PATH .) 例如,将它放在/usr/local/bin//opt/local/bin/ (系统PATH可以访问它们。)

If these conditions are met, you should be able to run it from anywhere on your local system as script_name.py 如果满足这些条件,您应该能够从本地系统的任何位置以script_name.py运行它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM