使用Cron运行Python脚本?

时间:2021-01-12 23:48:54

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? (say, I want it to run every minute).

我该如何将它添加到crontab? (比方说,我希望它每分钟运行一次)。

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.

重要提示:当我(使用shell)导航到脚本的文件夹时,我无法使用“./script_name.py”运行它;它不起作用。然而,当我使用“Python script_name.py”运行它时,一切正常。

1 个解决方案

#1


8  

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. #!/usr/bin/env python
  • 脚本的hash-bang设置正确,例如。 #!/ 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.)
    • 例如将它放在/ usr / local / bin /或/ opt / local / bin /中(系统路径可以访问它们。)

  • 它可从PATH访问,例如将它放在/ usr / local / bin /或/ opt / local / bin /中(系统路径可以访问它们。)

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运行它

#1


8  

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. #!/usr/bin/env python
  • 脚本的hash-bang设置正确,例如。 #!/ 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.)
    • 例如将它放在/ usr / local / bin /或/ opt / local / bin /中(系统路径可以访问它们。)

  • 它可从PATH访问,例如将它放在/ usr / local / bin /或/ opt / local / bin /中(系统路径可以访问它们。)

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运行它