linux - 如何让 CRON 调用正确的路径

我正在尝试让 cron 调用正确的路径。当我从 shell 运行 Python 脚本时,脚本运行良好,因为它使用 bashrc 中设置的 PATH,但是当我使用 cron 时,所有 PATH 都没有从 bashrc 中使用。是否有一个文件我可以为 cron 输入路径(如 bashrc)或从 bashrc 调用路径的方法?

对不起,我认为我的措辞不正确,我可以运行正确的脚本(这意味着 crontab 中脚本的 PATH 不是问题),只是当该脚本运行时我运行构建并这使用 .bashrc 中设置的 PATH。当我登录时运行脚本时,.bashrc 路径被拉入。由于 cron 没有在 shell 中运行,因此它不会拉入 .bashrc。有没有一种方法可以在无需编写 bash 脚本包装器的情况下实现这一点?

最佳答案

我使用了 /etc/crontab。我使用了 vi 并在我需要的路径中输入了这个文件并以 root 身份运行它。正常的 crontab 会覆盖您设置的 PATH。 A good tutorial on how to do this .

系统范围的 cron 文件如下所示:

This has the username field, as used by /etc/crontab.
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user   command
42 6 * * *   root    run-parts --report /etc/cron.daily
47 6 * * 7   root    run-parts --report /etc/cron.weekly
52 6 1 * *   root    run-parts --report /etc/cron.monthly
01 01 * * 1-5 root python /path/to/file.py

https://stackoverflow.com/questions/2388087/

相关文章:

linux - 检索最后 100 行日志

python - 如何将 python 日期时间转换为具有可读格式日期的字符串?

linux - 没有行号的 Bash 历史记录

linux - 每个虚拟主机的error_log?

python - 如何在使用 Python 插入 MySQL 数据库后获取 "id"?

python - Django 1.7 抛出 django.core.exceptions.AppR

linux - Mac 和 Linux 上的文本文件中的递归搜索和替换

python - 在进程运行时不断打印子进程输出

python - 将元组转换为列表并返回

linux - 如何找出适合由 shell 脚本解析的 linux 机器的总物理内存 (RAM)?