{"id":2519,"date":"2016-05-06T14:09:58","date_gmt":"2016-05-06T14:09:58","guid":{"rendered":"http:\/\/blog.designed79.co.uk\/?p=2519"},"modified":"2016-05-06T14:11:04","modified_gmt":"2016-05-06T14:11:04","slug":"raspberry-pi-init-d-run-app-on-boot","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=2519","title":{"rendered":"Raspberry Pi \/ init.d Run app on boot"},"content":{"rendered":"<p>Tutorial: How to run a program from boot<\/p>\n<p>Having a program start by itself when you boot your Raspberry Pi can be very useful. There are a few ways of running a command at start-up, but in this tutorial we will create a script within etc\/init.d so that when the system boots the program will start\/stop automatically on boot\/shut-down. <\/p>\n<p>For the purpose of this tutorial we will show you the method for an arbitrary program created in python &#8216;example.py&#8217; with its location \/home\/pi\/example.py. The following code will work for any script, just replace &#8216;example&#8217; with name of your program and replace \/home\/pi\/example.py to wherever you are actually keeping your script.<\/p>\n<p>First we need to create a script within \/etc\/init.d\/. Type the following command in the terminal but replace &#8216;example&#8217; with the name of your own program:<\/p>\n<p>sudo nano \/etc\/init.d\/example<\/p>\n<p>This will open a text editor. Paste the following code into here and replace the name and location of the program with your own. The the examples are the ones you&#8217;ll need to change:<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/><\/div><\/td><td><div class=\"bash codecolorer\"><span class=\"co0\">#! \/bin\/sh<\/span><br \/>\n<span class=\"co0\">#\/etc\/init.d\/example<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"co0\">### BEGIN INIT INFO<\/span><br \/>\n<span class=\"co0\"># Provides: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;example<\/span><br \/>\n<span class=\"co0\"># Required-Start: &nbsp; &nbsp;$remote_fs $syslog<\/span><br \/>\n<span class=\"co0\"># Required-Stop: &nbsp; &nbsp; $remote_fs $syslog<\/span><br \/>\n<span class=\"co0\"># Default-Start: &nbsp; &nbsp; 2 3 4 5<\/span><br \/>\n<span class=\"co0\"># Default-Stop: &nbsp; &nbsp; &nbsp;0 1 6<\/span><br \/>\n<span class=\"co0\"># Short-Description: start a program from boot<\/span><br \/>\n<span class=\"co0\"># Description: &nbsp; &nbsp; &nbsp; A simple script &nbsp;which will start a program from boot and stop upon shut-down<\/span><br \/>\n<span class=\"co0\">### END INIT INFO<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"co0\"># Put any commands you always want to run here.<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"kw1\">case<\/span> <span class=\"st0\">&quot;$1&quot;<\/span> <span class=\"kw1\">in<\/span><br \/>\nstart<span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Starting example&quot;<\/span><br \/>\n<span class=\"co0\"># run the program you want to start<\/span><br \/>\n<span class=\"sy0\">\/<\/span>home<span class=\"sy0\">\/<\/span>pi<span class=\"sy0\">\/<\/span>example.py<br \/>\n<span class=\"sy0\">;;<\/span><br \/>\nstop<span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Stopping example&quot;<\/span><br \/>\n<span class=\"co0\"># end the program you want to stop<\/span><br \/>\n<span class=\"kw2\">killall<\/span> example.py<br \/>\n<span class=\"sy0\">;;<\/span><br \/>\n<span class=\"sy0\">*<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp;<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Usage: \/etc\/init.d\/example {start|stop}&quot;<\/span><br \/>\n&nbsp;<span class=\"kw3\">exit<\/span> <span class=\"nu0\">1<\/span><br \/>\n&nbsp;<span class=\"sy0\">;;<\/span><br \/>\n<span class=\"kw1\">esac<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>Once you&#8217;ve copied the above code and replaced the names with that of your own exit and save with Ctrl+X.<\/p>\n<p>Next you need to make the program executable:<\/p>\n<p>sudo chmod +x \/etc\/init.d\/example<\/p>\n<p>Check the program is working correctly from etc\/init.d\/example by test starting it:<\/p>\n<p>sudo \/etc\/init.d\/example start<\/p>\n<p>And the same again test stop the program:<\/p>\n<p>sudo \/etc\/init.d\/example stop<\/p>\n<p>Next we need to register the program with the system so it knows to run\/stop the program at boot\/shutdown.<\/p>\n<p>sudo update-rc.d example default<\/p>\n<p>And that&#8217;s it! Now you can reboot your Raspberry Pi and the program &#8216;example&#8217; should start up automatically.<\/p>\n<p>If you ever wish to stop the program running from boot type the following command:<\/p>\n<p>sudo update-rc.d  -f example remove<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial: How to run a program from boot Having a program start by itself when you boot your Raspberry Pi can be [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2519","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2519"}],"version-history":[{"count":0,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2519\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}