0

I tried using cron to start python script , at boot (as mentioned in the link).Running a python script At Boot using cron

I made a python script "hello.py":

#!usr/bin/env python 
import time
print "Hello World!"
time.sleep(10)

Then chmod +x hello.py. I checked,if it running or not,it is giving me output. I used crontab -e command and added the line @reboot python /home/pi/hello.py &. Reboot using sudo reboot , but nothing happened! I am newbie .Although I read many discussions but I am not able to fix that!

8
  • 2
    how are you checking if your hello.py ran? because you won't see the printed "Hello World!". Try writing your "Hello World!" to a file and check if it's in the file ;-) Commented Dec 3, 2013 at 14:30
  • 1
    Or sleep for much longer ;-) Commented Dec 3, 2013 at 14:30
  • @Patrick I mean that if I ran that program on terminal using python hello.py it is running! and giving me output. Commented Dec 3, 2013 at 14:34
  • 1
    well the next thing i see is that the shebang is wrong: it is missing a / after !. other than that it's not really clear to me what you want :O Commented Dec 3, 2013 at 14:36
  • 2
    @Patrick is right, the problem seems like that you don't know the difference between foreground process and background process. In your case, running "python hello.py" in terminal is running a foreground process, and you can see the output directly from terminal. The cron version is a background process, the output can not be seen directly(that's why it is "background"), you can modify(crontab -e) your job as: python hello.py > /tmp/result.txt, the output will be "redirect" to /tmp/result.txt, and it'll prove that your script ran. Commented Dec 3, 2013 at 14:43

1 Answer 1

1

Generally when I want to verify whether a cronjob ran or not, I redirect all output to a log file like so:

12 0 * * * python /Path/To/File.py  > /Path/ToLog/log 2>&1

Then you can check this log timestamp and for its contents

Sign up to request clarification or add additional context in comments.

2 Comments

Yes! as @wentao explained, I verified this! :)
Ahh I hadn't seen @wentao 's explanation. It is spot on. Notice that I also redirect stderr with the 2>&1 This will put all stderr and stdout into the same file, which might or might not be what you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.