On WSL Ubuntu 20.04 I have two scripts (one sh and one python) that run perfectly if I launch them with bash, but not with cron.
The second (python) script is supposed to run just if the first (bash) succeded. I therefore used an && operator.
If I run /mnt/c/folder/first.sh && /home/myself/miniconda3/bin/python /mnt/c/folder/second.py, however if I have this very same line in crontab, the second script does not work.
I have tried to include the line /home/myself/miniconda3/bin/python /mnt/c/folder/second.py into the bash script, but also that doesn't work.
This python script makes use of an environment variable to work, I wonder if that is the reason. Although I have tried to include . $HOME/.profile; in the crontab right before the command above; without success.
1 Answer
This python script makes use of an environment variable to work, I wonder if that is the reason.
Yes, when executed via /bin/sh with a command (as anacron is doing by default), the shell is not a login shell, so ~/.profile doesn't get sourced.
Although I have tried to include . $HOME/.profile; in the crontab right before the command above; without success.
This worked for me. I added export abc=123 to my ~/.profile and added the following to /etc/crontab:
* * * * * username . $HOME/.profile && true && echo $abc >> $HOME/crontestThe resulting ~/crontest correctly displayed a 123 for every minute passed.
Another alternative would be to force the sh to run as a login shell (-l), and pass in your script via -c:
* * * * * username sh -lc 'true && echo $abc >> $HOME/crontest'