Crontab -e running script on startup (reboot script)


I am trying to get a python script to run at startup. @adam has been working with me on this and has been instrumental in boosting my knowledge and knowing what to look for on forums etc. I decided a new thread would be best.The script is designed to look for an input on pin 22 and reboot the Rpi3 when detected. The purpose is to allow my backup solar PLduino to reboot the Rp3 in case I lose communication with it.

I did as adam suggested and added @reboot python /home/pi/ to crontab -e but the crontab is empty otherwise as if cron services is ignoring doesn’t use crontab it or something about the script won’t be allowed to run on startup. A forum talks about virtual console using pi root user sudo /bin/bash. When I get to workon cv it gives me command not found. Do I need to download something like open cv3 or other?

source /home/pi/.profile
workon cv
cd /home/pi/pi-reboot (maybe leave this at /home/pi where my file resides or create that folder and move file there?)
python to show my file)

Any help is appreciated,


Hello sir,
I have like 10 or more scripts using @reboot in crontab.
I added them as you specify - @reboot and then path to filename.

I added them as the command that I use is: sudo crontab -e


I added them same way before and nothing happens.


Does the script work if you run it from the terminal? Running at reboot is not going to give you any output so if something is failing you will never know.


Here’s the output from:
chmod a+x (your script)
Is this the correct mode to run in terminal?

your script:
lines 1 thru 4 import: command not found. line 5 ./r’ command not found. line 6 syntax error near unexpected token `(’) def reboot(channel)
the other script (the one I found and modified pin assignment) gives the same basic " import: command not found " and lists some directories for mail process which may have been set up to email the coder the output when running the script for error checking. This makes me believe I need to trim down that one script or maybe yours needs tweaking?

thanks William


If you post the output I’ll be able to tell you what you need to install. Without it it’s just guessing 1/4 libraries that need installed. time, os, sys, and rpi.gpio should all be installed by default on Jessie full.


Here tis


use the command python


get back “pin value: 0” repeatedly. I applied 3.3v to pin and still 0.


Do you have a common ground between your power sources?


only one power source at this point, Rpi.


I’d say at that point you need to figure out why the pin isn’t changing value. Check your connectors, make sure there really is 3.3v where you think there is, check to see if the pin is the right pin, etc.


I’m connected with a cobbler and def has 3.3v. Verified correct pin. The other script I used will reboot when pull up on pin but like yours it will not work at reboot.


The other script is using bcm pin numbering and I’m using board numbering. If I had to guess you are on the wrong pin. There’s no way one works and one doest, it’s using the same exact library.


Ok so my old brain was being dumb. Using a hit and miss procedure I found gpio 23 is actually pin 16(pin I chose in your script).

I couldn’t get it to run on startup/reboot so I added sudo python /home/pi/ & just before the last line in /etc/rc.local. Works like a champ.

A thought crossed my mind, crontab didn’t work for me because I actually wrote a new file. Should have: sudo nano /etc/crontab -e to edit the actual file. :thinking:

Thanks for your help. I was determined to find a way to prove your script works since obviously you’ve been at this longer than I.

Treat me like a really young student or dense old geezer and I shall learn well😊


You shouldn’t have to use sudo nano /etc/crontab -e. When you use the command crontab -e it will ask you to choose your editor and create the new file for you when you save.


Finally getting around to adding This reboot python script in this thread.

Credit goes to @adam for helping write this code. Took some forehead smacking but adam came thru and I got the code in the right place and when I click button to reboot Rpi it works flawlessly.