简体   繁体   English

无法在 cronjob 中使用 source.bashrc

[英]Cannot source .bashrc inside cronjob

I'm trying to run a python script on a cronjob.我正在尝试在 cronjob 上运行 python 脚本。 To access environment variables in that script, I've wrapped the python script in a short shell script.为了访问该脚本中的环境变量,我将 python 脚本包装在一个简短的 shell 脚本中。 However, cron does not appear to be accessing my environment correctly, as I'm still getting errors in my python script about not being able to access environment variables.但是,cron 似乎没有正确访问我的环境,因为我的 python 脚本中仍然出现关于无法访问环境变量的错误。 If you consider the below shell script如果您考虑以下 shell 脚本

#!/bin/bash
source /home/jfeldman/.bashrc
env

Running this script from a cronjob yields only this output从 cronjob 运行这个脚本只会产生这个 output

SHELL=/bin/sh
PATH=/usr/bin:/bin
PWD=/home/jfeldman
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/jfeldman
LOGNAME=jfeldman
_=/usr/bin/env

Whereas running that shell script from the command line yields a much larger list of environment variables, including the API tokens that I want to access from my python script.而从命令行运行 shell 脚本会产生更大的环境变量列表,包括我想从 python 脚本访问的 API 令牌。 Additionally, my PATH variable is considerably shorter than the cronjob version of env此外,我的 PATH 变量比env的 cronjob 版本短得多

Does anyone know why Cronjobs couldn't access my environment correctly even if the cronjob is running a script that explicitly declares source /home/jfeldman/.bashrc ?有谁知道为什么 Cronjobs 无法正确访问我的环境,即使 cronjob 正在运行明确声明source /home/jfeldman/.bashrc的脚本?

This is the line from the crontab fwiw */5 * * * * /home/jfeldman/crons/test_cron.sh >> /home/jfeldman/crons/test.log 2>&1$`这是来自 crontab fwiw */5 * * * * /home/jfeldman/crons/test_cron.sh >> /home/jfeldman/crons/test.log 2>&1$` 的行

Edit: after some testing it seems like cron can source my virtual environment just fine, just not.bashrc, which seems incredibly bizarre.编辑:经过一些测试,似乎 cron 可以很好地获取我的虚拟环境,只是 not.bashrc,这看起来非常奇怪。

Shell sh is not bash . Shell sh不是bash sh doesn't know the command source and possibly wouldn't import .bashrc without errors when using it's source command . /home/user/.bashrc sh不知道命令source ,并且在使用它的源命令时可能不会导入.bashrc而不会出错. /home/user/.bashrc . /home/user/.bashrc for sourcing it. . /home/user/.bashrc用于采购它。

Put the line放线

SHELL=/bin/bash

on top of your crontab to run the jobs with bash.在您的crontab之上使用 bash 运行作业。

Sourcing a users .bashrc to a cronjob running as root user is a bad idea, you could think about creating a job in /etc/cron.d/ instead, and specify a user other than root running the job.将用户.bashrc提供给以 root 用户身份运行的 cronjob 是一个坏主意,您可以考虑在/etc/cron.d/中创建一个作业,并指定一个非 root 用户来运行该作业。

5/* * * * * jfeldman /home/jfeldman/cron_runner.sh

.bashrc can change over time it makes sense to copy everyting into the cron_runner.sh or even better - create a new user with own home dir just for running the cronjob in a seperate home dir. .bashrc可以随着时间的推移而改变,将所有内容复制到cron_runner.sh甚至更好 - 创建一个具有自己主目录的新用户,只是为了在单独的主目录中运行 cronjob。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM