使用 Cron 运行 Python 脚本? [英] Running a Python Script using Cron?
问题描述
我有一个想要添加到 cron 的 python 脚本.
I have a python script that I'd like to add to cron.
该脚本对其具有 +x 权限.
The script has +x permission on it.
如何将其添加到 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.
推荐答案
从 cron
开始,您应该将脚本作为 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 systemPATH
.)
如果满足这些条件,您应该可以在本地系统的任何位置以
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屋!
- e.g. place it in
- 例如将它放在