python脚本div in crontab中设置的拾取环境变量
当我从终端运行Python脚本时,由于缺少ENV变量,我会出现错误。我可以使用导出
来解决此问题并进行设置。但是,还有其他一些python脚本也像cron作业一样运行良好。我看到此ENV变量在Cron选项卡中设置。因此,我的问题是,是否可以使用CRON中的ENV变量设置,用于由CLI的用户/root运行的脚本?运行env
命令作为root不显示此变量。
When i run my python script from terminal i get error due to missing env variable. i can fix by this using export
and setting it. But there are other python scripts that are running fine as cron jobs that require this as well. And i see this env variable being set in cron tab. So my question is if env variable set in cron is available for scripts run by user/root from cli? Running env
command as root doesn’t show this variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
环境变量是每个过程的私有,而不是全局。当启动新的过程时,它将从其父母那里继承其初始环境。您的终端外壳不是由Cron启动的,因此它不会继承您在crontab中设置的env var。
您的操作系统可能会为所有进程设置Env VAR提供标准机制(例如,您的终端启动您的CLI Shell)。我更喜欢做的是创建一个名为〜/.environ 的文件,然后我在我的〜/.bashrc 和cron作业中
source
。Environment variables are private to each process, not global. When a new process is started it inherits its initial environment from its parent. Your terminal shell isn't started by cron so it doesn't inherit the env vars you've set in your crontab.
Your OS may provide a standard mechanism for setting env vars for all processes (e.g., your terminal which then starts your CLI shell). What I prefer to do is create a file named ~/.environ that I then
source
in my ~/.bashrc and cron jobs.