Bainly
Bainly

Reputation: 87

How can I get crontab to work properly in Raspbian?

I am new to crontab and I'm struggling with the basics. I a lot of different issues. This is all being done on my Raspberry Pi. I am trying to schedule a python script to run every 10 minutes on weekdays. What makes this trickier is that my python script needs to use a virtual environment.

After doing some research I saw I could just activate the virtual environment with a bash script and then run the python script through that. This is the bash script:

#!/bin/bash

cd /home/pi/Desktop/projects/my_project

source env/bin/activate

python my_script.py

I have no idea if this is the best way to run python in a virtual environment through crontab but its all I could find online. This is the code for the crontab itself:

SHELL=/bin/bash

0-59/10 * * * 1-5  /home/pi/Desktop/projects/my_project/cron_script.sh

I tested the crontab code by running this in the command line: sudo crontab -l | grep -v '^#' | cut -f 6- -d ' ' | while read CMD; do eval $CMD; done. Again I have no idea if this is the best way to test it immediately. This throws an error message:

bash: /home/pi/Desktop/projects/my_project/cron_script.sh: Permission denied

I'm really just confused at a lot of different steps here. Summary:

  1. I don't know if I'm using a python virtual environment in crontab correctly or whether there's a cleaner way to do it.
  2. I don't know if there is a better way to test crontab immediately. Writing the print statements from my python script to a txt file would also be nice.
  3. I'm getting a permission denied error message.

Upvotes: 0

Views: 512

Answers (1)

sodimel
sodimel

Reputation: 916

The error you're having is likely due to the fact that your bash script is not marked as executable. You can use chmod to change access permissions.

Try this :

chmod +x /home/pi/Desktop/projects/my_project/cron_script.sh

Upvotes: 1

Related Questions