2

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.

6
  • 1
    Put SHELL=/bin/bash near the top of your crontab.
    – user2849202
    Commented Jun 29, 2020 at 20:18
  • no dice unfortunately @Roadowl. source isn't throwing an error which suggests that it's not using sh, but something is still going wrong
    – jfeldzy
    Commented Jun 29, 2020 at 21:01
  • I wonder which env.vars you need in the python script.
    – user2849202
    Commented Jun 29, 2020 at 21:15
  • 1
    How are you setting the environment variables within your bashrc? Commented Jun 29, 2020 at 21:53
  • Without showing the contents of ~/.bashrc, it's a guessing game to say what's happening. Commented Jun 29, 2020 at 22:31

1 Answer 1

0

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.

2
  • I already tried this without success unfortunately. Even if I don't add that, I think adding the bash shebang should have solved that. As i noted above, the fact that source isn't throwing an error suggests that I am in bash I've used the cron_runner suggestion as a workaround before but I'm also getting PATH-related problems that I think stem from the same underlying problem so I figured I'd throw the env vars problem on stack overflow bc it's the easiest to explain version of the problem
    – jfeldzy
    Commented Jun 30, 2020 at 15:06
  • SHELL is the variable that tells cron what program to use to run the crontab entries. The shebang has nothing to do with that. Yes you need to set the PATH variable.
    – Michael D.
    Commented Jun 30, 2020 at 21:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.