-1

I've been trying to get this to work for DAYS. Its doing my head in because everything i search suggests this work.

The process is Cron executes bash which executes python

I open and edit the cron with:

sudo crontab -e

Inside it looks like this:

* * * * * /home/pi/cron_bash.sh >> /home/pi/out.txt  2>&1

This then feeds through to the bash file which looks like this:

#!/bin/sh
cd "$(/home/pi "$0")";
CWD="$(pwd)"
echo $CWD
python /home/pi/cron_log.py

The python script is very simple (primairly for debugging process, i'm planning on changing to a script that executes selenium when i've found the issue)-just prints the datetime to a file:

from datetime import datetime
dateTimeObj = datetime.now()   
dateStr = dateTimeObj.strftime("%B the %d of %Y is %A at %I:%M %p")
with open('cronlog.txt', 'a') as file:
    file.write(dateStr)

The problem i have is that the output file (cronlog.txt) doesn't get changed. If i check the out.txt file which should be displaying the log i get this:

/home/pi/cron_bash.sh: 2: /home/pi/cron_bash.sh: /home/pi: Permission denied
/root

Can anyone help? I don't understand how it could be a permission issue if i'm going through sudo cron, but then again i am a beginner with pi/cron.

Thanks

Felix

2
  • Why does your program need to update a crontab that it doesn't own? Stop using sudo when you don't need to use it. Every user can have a personal crontab (edited with crontab -e or updated with echo "* * * * * somecommandhere" >> /tmp/cronup; crontab /tmp/cronup;
    – Dougie
    Feb 22 at 16:30
  • I think you misunderstood what the crontab is doing, its launching a bash which launches python. Obviously I tried using the personal crontab first but I ran into the same pwrmission error
    – Felixr2
    Feb 23 at 22:09
0

This line in your script:

cd "$(/home/pi "$0")";

is trying to execute /home/pi as if it's a program, but it's surely a directory. What did you mean this line to be?

4
  • Maybe you meant cd /home/pi?
    – Waxrat
    Feb 22 at 17:05
  • I was actually following the advice from this previously answered question stackoverflow.com/questions/12534135/…
    – Felixr2
    Feb 23 at 22:00
  • But I'm not sure what the cd line does in its entirety, I assumed just change directory
    – Felixr2
    Feb 23 at 22:07
  • I think you maybe just want cd /home/pi which would change your working directory to the pi user's home directory. Or cd $(dirname "$0") which would change your working directory to the directory where the bash script is.
    – Waxrat
    Feb 24 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.