子进程上的路径 check_output

Path on subprocess check_output

使用 crontab 执行 python 脚本时,脚本缺少环境变量。

该脚本调用一个可执行文件,其中一些 Linux 发行版在 /usr/bin 下发布,而其他发行版在 /sbin.

下发布

subprocess.check_output() 没有说明这一点。

我可以假设 which 总是在 /usr/bin/which 之下吗? Popenenv=os.environment.copy() 的更好选择吗?

我已经尝试在 crontab 中使用 shell=Truesh -c

#read the Kernel's routing table by calling /usr/bin/route -n
routes = str(subprocess.check_output(["route", "-n"]), "utf-8")

https://github.com/pzillmann/wireguard-dynamic-routing/blob/master/wg-dynroute.py#L32

我希望脚本在 bash 会话中调用和在 crontab 中调用时表现出相同的行为。

FileNotFoundError: [Errno 2] No such file or directory: 'route'

When executing a python script with crontab, the script lacks environment variables.

这可能取决于我们谈论的 cron。 Vixie cron 设置 PATHHOMESHELL 以及 LOGNAME.

LOGNAME 之外的任何内容都可以在 /etc/crontab 或您自己的 crontab 文件中覆盖。因此,在您的 crontab 文件中设置适当的 PATH