Search code examples
pythoncronraspberry-pi

Raspberry Pi Crontab Python script needs write permission


I have a python script that I would like to run on my raspberry pi every 15 minutes. The script should do something and then post the reports into a txt file. When I run the script using

sudo ./automate.py

everything works fine. But my crontab never posts a report. I checked the crontab logs, and the script is running; however, it isn't posting anything to the txt files (One of which just appends "Executed at time", so it should post every time).

When I run the script without sudo, I get an error saying I do not have permission to write to a the file, so I assume that is the problem.

I have tried the following in crontab:

*/15 * * * * python /path/automate.py

*/15 * * * * sudo python /path/automate.py

15 * * * * python /path/automate.py

15 * * * * sudo python /path/automate.py

I have created these crontab scripts for both the user (which has total permissions) and the root user (pi) with the same problems.

I also should mention that the files are on an external HDD, but both accounts have write permissions to the drive so I doubt that was the problem.


Solution

  • I needed to change the directory in the crontab command to the working directory of the project. Making it:

    cd /path/to/file; python automate.py