如何让 CRON 调用正确的路径 [英] How to get CRON to call in the correct PATHs

查看:15
本文介绍了如何让 CRON 调用正确的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get cron to call in the correct PATHs. When I run a Python script from shell the script runs fine as it uses the PATHs set in bashrc but when I use cron all the PATHs are not used from bashrc. Is there a file I can enter the PATHs into for cron like bashrc or a way to call the PATHs from bashrc?

抱歉,我认为我的措辞不正确,我可以运行正确的脚本(意味着 crontab 中脚本的 PATH 不是这里的问题),只是在该脚本运行时我运行了构建和这使用 .bashrc 中设置的 PATH.当我在登录时运行脚本时,.bashrc PATH 被拉入.由于 cron 不在 shell 中运行,所以它不会拉入 .bashrc.有没有一种无需编写 bash 脚本包装器就可以将其引入的方法?

Sorry I don't think I worded this correctly, I can get the correct script to run (meaning the PATH to the script in crontab is not the problem here), it's just when that script is running I run a build and this uses the PATHs set in .bashrc. When I run the script when I'm logged in, the .bashrc PATHs are pulled in. Since cron doesn't run in a shell per say it does not pull in .bashrc. Is there a way of pulling this in without having to write a bash script wrapper?

推荐答案

我使用了 /etc/crontab.我使用了 vi 并在这个文件中输入了我需要的 PATH 并以 root 身份运行它.正常的 crontab 会覆盖您设置的 PATH.一个很好的教程关于如何做到这一点.

I used /etc/crontab. I used vi and entered in the PATHs I needed into this file and ran it as root. The normal crontab overwrites PATHs that you have set up. A good tutorial on how to do this.

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

The systemwide cron file looks like this:

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

这篇关于如何让 CRON 调用正确的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆