Cannot source .bashrc inside cronjob

Viewed 178

I'm trying to run a python script on a cronjob. To access environment variables in that script, I've wrapped the python script in a short shell script. 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. If you consider the below shell script

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

Running this script from a cronjob yields only this 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. Additionally, my PATH variable is considerably shorter than the cronjob version of env

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?

This is the line from the 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.

1 Answers

Shell sh is not 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 for sourcing it.

Put the line

SHELL=/bin/bash

on top of your crontab to run the jobs with 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.

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.

Related