[英]Running python script from crontab?
I need some help running a python script from crontab: 我需要一些从crontab运行python脚本的帮助:
The script looks for subfolders from current path and does something to them, also extracts a zip file located in the same folder of the script into each found subfolder. 该脚本从当前路径中查找子文件夹并对它们执行操作,还将位于该脚本相同文件夹中的zip文件提取到每个找到的子文件夹中。
When I go with cd /folder/folder
then python script.py
is all good. 当我使用cd /folder/folder
python script.py
很好。 But when run it with crontab it runs in users home folder and not where the script is placed. 但是,当使用crontab运行它时,它将在用户的主文件夹中运行,而不是在脚本所在的位置。
To overcome this I placed in crontab something like this: 为了克服这个问题,我在crontab中放置了以下内容:
* * * * cd /folder_of_scrpit/ && /python_path/python script.py >> log.txt
and works as needed but feels weird, is there a better way of achieving this? 并根据需要工作,但感觉很奇怪,是否有更好的方法来实现这一目标?
You can cd
in crontab the way you do it. 您可以按照自己的方式在crontab中进行cd
刻录。 Or you can call os.chdir()
in your script. 或者,您可以在脚本中调用os.chdir()
。 In the latter case you can write the directory in the script or pass it as a command line argument: /python path/python script.py /folder/folder
. 在后一种情况下,您可以在脚本中写入目录或将其作为命令行参数传递: /python path/python script.py /folder/folder
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.