Crontab CD to Directory

时间:2016-04-21 22:24:31

标签: cron crontab cron-task

All of my cronjob scripts run from a specific directory. Can I add cd /FOLDER/PATH at the top of the crontab file and expect all scripts to be run from that directory?

Currently all my crontab functions are like this (ignore lack of specific run frequencies)

* * * * * cd /FOLDER/PATH && python3 File.py
* * * * * cd /FOLDER/PATH && python3 File2.py
* * * * * cd /FOLDER/PATH && python3 File3.py

I would rather it be like

cd /FOLDER/PATH
* * * * * python3 File.py
* * * * * python3 File2.py
* * * * * python3 File3.py

1 个答案:

答案 0 :(得分:0)

由于crontab文件实际上并不是在触发任务时正在运行的内容,因为从cron守护程序运行cron任务,您将要执行的操作将无法工作。一个美化命令的解决方案是添加路径的实际脚本是crontab文件上的PATH env变量

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/path/to/scripts

然后做这样的事情:

* * * * * /FOLDER/PATH/File.py
* * * * * /FOLDER/PATH/File2.py
* * * * * /FOLDER/PATH/File3.py

请注意,name.py文件应该在文件顶部定义解释器才能使用。