使用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 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
- e.g. place it in
- 例如将它放在