0

I have a python file that I would like my Raspberry Pi 3 B to run, the instant that I plug the power cable in. I dont want to have to log in or connect a computer to it before the file starts running. Ive looked around on the internet and a lot of them say to edit the /etc/inittab file, which is a file I dont have on my Raspberry pi. Ive also tried editing raspi-config and set the login to automatic but it still asks for a password when ssh ing in.

Please could someone help me with this?

edit: I didnt mean the "instant" literally, I just would like for it to run without me doing anything to it after it is plugged in.

mnt
  • 93
  • 7

1 Answers1

1

It can not be done.

When power is applied the Pi will boot. The boot process takes several seconds. As part of the boot process the operating system (normally Linux) is launched. You can only run a Python script once the operating system has launched.

Depending on the Pi model this will be, say, 10-30 seconds after power has been applied.

joan
  • 71,024
  • 5
  • 73
  • 106
  • Sorry my bad. How do I set it so that it will run 10-30 seconds after it receives power? – mnt Mar 07 '17 at 16:31
  • Many ways. Search for how to launch programs at start-up. Personally I'd add the script to crontab using the @reboot line. – joan Mar 07 '17 at 16:39
  • Ive just tried this http://www.raspberrypi-spy.co.uk/2013/07/running-a-python-script-at-boot-using-cron/

    after I do sudo reboot, should the program run when I plug and unplug the raspberry pi?

    – mnt Mar 07 '17 at 16:45
  • Yes, provided you have updated the crontab correctly and your script is suitable to run in the background (i.e. doesn't expect keyboard input and doesn't write results to the screen). – joan Mar 07 '17 at 16:49
  • Thanks for your replies Joan, when I sudo reboot. Then the program only runs when I sign in via ssh. Is this some automatic log in issue? I cant seem to enable it using raspi-config – mnt Mar 07 '17 at 16:51
  • @mnt I don't know. I added the following line to my (pi user) crontab @reboot echo "hello" >>/home/pi/cronfile and the file was created before I logged in (I checked the file time-stamp). – joan Mar 07 '17 at 17:18
  • @mnt: That sounds like you added it to the .baschrc file instead of crontab... – Jacobm001 Mar 07 '17 at 17:24