Start python script from init.d

1. place your python script in the following folder: /usr/local/sbin/
2. create a new file in /etc/init.d/ in this case it will be example, so sudo nano /etc/init.d/example
3. Use the following code for the example file

4. give the file execute rights, sudo chmod 755 example
5. now execute: sudo update-rc.d example defaults

When you reboot debian it should automatically start the python script, else try service example start and service example stop

Comments: 20

  1. Heinrich April 20, 2013 at 15:44 Reply

    Found it while searching a method to start automatically a python script on my new raspberry pi. Works fine now and records outside temperature to a mysql database.

    Thanks for your excellent help


    • p.vanos April 20, 2013 at 21:14 Reply

      Your welcome :-),


  2. Kamil Hashmi June 16, 2013 at 12:54 Reply

    Thank you so much. I found it so straight forward and easy to understand.

  3. Bogdan Ionescu June 22, 2013 at 13:44 Reply

    After hours of searching the internet this finally helped me
    i used it to starts a cooling fan when the r pi boots because when i was connecting it directly to 5v it would continue to spin after the rpi was halted
    thanks so much

  4. Dima July 9, 2013 at 08:07 Reply

    “killall python” <—– have five! =)

    • p.vanos July 9, 2013 at 23:18 Reply

      Haha, yes it was a quick solution. I will fix it.

  5. Andrew Webb October 12, 2013 at 10:44 Reply

    1. place your python script in the following folder: /user/local/sbin/

    shouldn’t this be: /usr/local/sbin/

    • p.vanos October 12, 2013 at 10:46 Reply

      Ah, thanks I didn’t recognize the typo.

  6. joeM March 27, 2014 at 21:56 Reply

    Great tutorial, tested on a pcDuino with Ubuntu 12 and working great! Thank you for the tutorial was very useful and simple to follow!

  7. Len April 15, 2014 at 01:39 Reply

    What’s the fix for the killall? How do you stop just 1 of ???


    • p.vanos April 15, 2014 at 09:05 Reply

      I have the new code on my laptop.
      I will update it this week.

    • mrt-prodz June 3, 2014 at 13:31 Reply

      To kill the specific python script you need to list current process running, grep your script, and keep the process id from the output.

      kill $(ps aux | grep “python /usr/local/sbin/” | awl ‘{print $2}’)

  8. Mostafa April 21, 2014 at 10:47 Reply


  9. barry May 11, 2014 at 09:33 Reply

    First example that worked for me after having tried some others. It starts my smartmeter monitoring py script. Looks like it restarts the script after being disconnected for some reason. Very nice, thanks a lot.

  10. […] Testing of my python script that reads the serial output from MotherHub seems stable enough to run fulltime now. What I’ve found is though that everytime I turn my PC off my SSH session will time out and kill the python script from running. What I need is a way to run it all the time and auto start it after a reboot. Peiter Vanos has just the thing! I am reposting here for my own reference later. You can find the original code at […]

  11. alexmrb July 31, 2015 at 17:35 Reply

    Could you please help me? Upon entering sudo update-rc.d example defaults, I’m getting an error which says “missing LSB overrides.” Any help would be appreciated as I am SUPER new to Linux

    • Jay Doshi March 20, 2016 at 18:13 Reply

      I had the same problem,
      Go to the skeleton file in the init.d folder.
      If look at the /etc/init.d/skeleton script, in the beginning of it there are two marks : ### BEGIN INIT INFO and ### END INIT INFO
      The easiest way to fix the warning, is to copy the LSB tags from the skeleton script to the beginning of your blah script and just change skeleton to blah

  12. ASP February 9, 2016 at 15:33 Reply

    I used this to set python data logger to input information from temperature sensor to MySQL db on Raspberry Pi 2 with Debian on it – and it was the first thing that actually worked (other than obvious console run). The problem is that the code run for about 2 minutes and then just stopped. Since I do not see anything about the duration period in the code I was wondering if there was any advice from the more experienced as to what could be the issue here? Thanks in advance!

  13. Karteek Paruchuri February 19, 2016 at 00:02 Reply

    I’ve two python scripts which run two httpwebservers for listening to incoming requests. I hooked those two python programs to this script and started the service. if I kill the process manually, will the script bring back those again automatically? If not what additional info do I need to add to get this in place. Thanks in advance. And this script works very well to trigger the python programs

  14. Andrew Servi May 15, 2016 at 00:46 Reply

    I didn’t have much luck with this, although I could create the service, and manually start and stop it. My issue was that I could not get it to start automatically on boot. I think it didn’t work because I’m running a newer version of Debian.

    Here is the link to the solution that worked for me.

Leave a Reply

Your email address will not be published. Required fields are marked *