1

EDIT 2 27/06/2013: The problem was a silly mistake unrelated to venv and cron. Running venv from cron with the same user that created the venv works great using the activate desscribed below.

EDIT 25/06/2013: Since nohup.out is unchanged after the cron runs, I suspect the problem is in the use of virtualenv. The cron is set with the same user than the one running the script from the command line.


I have written a script to activate venv then run a python script. It runs perfectly from the command line, when I do

nohup /home/heyheyhey/run.sh &

However, for some black magic reasons, the python part does not run from cron:

0 4 * * * /home/heyheyhey/run.sh &

Content of run.sh:

#! /bin/bash
cd /home/heyheyhey
. /home/heyheyhey/.pythonbrew/venvs/Python-2.7.2/venvname/bin/activate 2> error.log
python /home/heyheyhey/top.py 2> error.log
bzip2 -c "Exporter.csv" > "extraction.csv.bz2"

The run.sh executes since the output compressed file is created. However the python script does not work since the Exporter.csv is not updated and I do not see activity into the log file.

Thanks for your help!

Antoine Brunel
  • 1,065
  • 2
  • 14
  • 30
  • 1
    There's no need to use `nohup` or `&` in a cron job. `nohup` isn't necessary because there's no terminal to disconnect, and `&` isn't needed because everything in cron is effectively in the background. – Barmar Jun 25 '13 at 00:22
  • Yes, I agree, but I tried with and without nohup and there are no differences, the python script still does not run! – Antoine Brunel Jun 25 '13 at 08:50
  • 1
    I never said it would make a difference, I just said it's not necessary. – Barmar Jun 25 '13 at 15:00

1 Answers1

2

The environment of a cron job is often different from the environment you see when you're logged into an interactive shell. In particular, you might want to check whether the python interpreter is on the $PATH for the cron job. If your python program reads any environment variables, you should check those too, to ensure they're set as expected under cron.

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96