I have created a python script that lights up some leds on a bit of breadboard then it's supposed to shut down the pi, the script works fine if I run it in python and the pi shuts down as intended.

I created a crontab using sudo crontab -e

@reboot /path/to/myFile.py

And chmod a+x myFile.py

On boot the script runs just fine and the leds light as intended but it just will not shutdown at the end of the script, spent the last two days trying to figure this out. The shutdown part looks like this

os.system("lxterminal -e sudo shutdown -h now")

but it seems cron is ignoring this, been reading around and can't seem to find the answer anywhere but from what I understand now cron won't open a terminal

I tried making a file in /home/pi/.config/autostart/ that would run the file on startup, if I double click the file it runs but on reboot nothing. Tried chmod and still nothing. Tried making a separate file that shuts down after 5 minutes and tried all different things with that but to no avail, driving me crackers.

How can I get the pi to shutdown at the end of my python script using cron?

1 Answer 1


If you want it to shutdown after your script ends, why not invoke the shutdown command directly?

os.system("sudo shutdown -h now")

There's no point in adding more complexity. Basically, what I'm saying is remove lxterminal -e.

Also, fix your cron entry.

@reboot /usr/bin/python /path/to/myFile.py

After that, undo everything else (e.g. the autostart thing).

  • Worked like a charm thanks, didn't think to try that without terminal, been away from python for a while and still a bit rusty, coming back to me gradually though.
    – Jon Wilmot
    Commented Jan 29, 2017 at 15:56

Your Answer

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

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