0

Struggling to get crontab to execute a shell script on my Raspberry Pi. The script runs fine if executed with bash (it's executing a python script that runs in a virtual environment).

Here's what I've tried

crontab -e
* * * * * /home/pi/bash/myshell.sh

And the shell script...

#! /bin/bash
cd /home/pi/projects/scripts
source activate myEnv
python pyscript.py
source deactivate

This works fine...

bash /home/pi/bash/myshell.sh

I also tried editing the root cron tab file directly with sudo nano /etc/crontab but this also didn't work. What am I missing here?

Muon
  • 1,294
  • 1
  • 9
  • 31

3 Answers3

1

You need to give executable permissions to the shell script:

chmod u+x /home/pi/bash/myshell.sh

The above sets permission for the file to be executed only by the owner or by the root user.

Exadra37
  • 11,244
  • 3
  • 43
  • 57
  • Thanks @Exadra. Should have mentioned I completed this step already. Does this look right? `ls -la` `-rwxr-xr-x 1 pi pi 187 Oct 4 15:29 myshell.sh` Still not tunning. – Muon Oct 04 '19 at 15:35
  • Well everyone have permission to execute the bash script and that is not good from a security point, but that will not be the reason for not working from cron. – Exadra37 Oct 04 '19 at 15:57
  • Try to use `sh` instead of `bash` in your shell script, like `#!/bin/sh`. – Exadra37 Oct 04 '19 at 15:59
  • Hey, just solved it. It was a problem with the Python virtual environment and not crontab at all, my bad. Removing the source activate and replacing the python line with `/home/pi/berryconda3/envs/myEnv/bin/python pyscript.py` worked. Thanks for your input. – Muon Oct 04 '19 at 16:06
  • 2
    Just add your own answer, and mark it as the accepted one. This will facilitate others to solve a similar issue. – Exadra37 Oct 04 '19 at 16:15
1

Thanks for everyone that took the time to answer. So the problem was not with crontab at all but was actually an issue with Python virtual environments. Using source activate was not working with crontab. The solution was to modify the shell script so that it directly uses the Python in the specified virtual environment. For example..

#! /bin/bash
cd /home/pi/scripts
/home/pi/berryconda3/envs/myEnv/bin/python pyscript.py

Also I had logging done within Python but because it crashed early in the script they were never written so I incorrectly assumed the crontab was not executing correctly. I have since discovered that it is a very good idea when working with crontab to log what's going on directly from cron like this...

* * * * * /home/pi/bash/myshell.sh >> /home/pi/logs/cronlog 2>&1

This way I could actually see what was going wrong (i.e. Python was throwing ModuleNotFoundError).

There's a much better answer on this here that is worth looking at. I'll mark this question as a duplicate.

Muon
  • 1,294
  • 1
  • 9
  • 31
0

Set path at the beginning of your script - for example:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
UtLox
  • 3,744
  • 2
  • 10
  • 13