{"id":678,"date":"2012-10-20T11:25:25","date_gmt":"2012-10-20T11:25:25","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=678"},"modified":"2024-01-31T13:17:53","modified_gmt":"2024-01-31T13:17:53","slug":"auto-running-programs","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/pi-operating-systems\/raspbian\/auto-running-programs","title":{"rendered":"Auto Running Programs-Command Line"},"content":{"rendered":"<h4>\nAuto running applications in the GUI<br \/>\n<\/h4>\n<p>\nSee <a href=\"https:\/\/raspberry-projects.com\/pi\/pi-operating-systems\/raspbian\/auto-running-programs-gui\" target=\"_blank\" rel=\"noopener\">here<\/a>\n<\/p>\n<h4>\nAuto running terminal applications (non GUI)<br \/>\n<\/h4>\n<p>\nFirst ensure your program is executable&nbsp;by finding it in the file manager. &nbsp;Right click on the file and select properties.&nbsp; Select the permissions tab, check the &lsquo;Make the file executable&rsquo; box and press OK. &nbsp;Or&nbsp;from the command line use:\n<\/p>\n<pre>\r\n<code>\r\nsudo chmod +x \/home\/pi\/projects\/my_project.a\r\n<\/code><\/pre>\n<p>\nOr using a different tool set its chmod to 755 (rwxr-xr-x). &nbsp;It doesn&#39;t matter if the user is root.\n<\/p>\n<p>\n<em><strong>Doesn&#39;t work?<\/strong><\/em>\n<\/p>\n<p style=\"margin-left: 40px;\">\nWe&#39;ve found that when copying a new file to the rpi&nbsp;using WinSCP, changing its properties to 755 and verifying all is OK that if we kill the power and power up again the executable doesn&#39;t run. &nbsp;However if we use sudo reboot at the command line it works as it should. &nbsp;It seems there is some sort of caching action going on so after doing this use sudo reboot the first time&nbsp;rather than cycling the power!\n<\/p>\n<p>\nYou can setup the auto run using a script (see <a href=\"https:\/\/raspberry-projects.com\/pi\/pi-operating-systems\/raspbian\/scripts\" target=\"_blank\" rel=\"noopener\">here<\/a>), or you can do it directly by editing the rc.local file:\n<\/p>\n<pre>\r\n<code>\r\nsudo nano \/etc\/rc.local\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\nAfter the initial comments (lines beginning with &#39;#&#39;) add the following lines:\n<\/p>\n<pre>\r\n<code>\r\n# Auto run our application\r\nsudo \/home\/pi\/projects\/my_project.a &amp;\r\n<\/code><\/pre>\n<p>\n&quot;sudo&quot; assumes you want your application run with root user privileges (remove if not) and the &quot;&amp;&quot; says do it in the background.\n<\/p>\n<p>\nSave it by pressing Ctrl+X, &quot; Y&quot;, ENTER\n<\/p>\n<p>\nRe-boot your RPi and it will run.\n<\/p>\n<h4>\nTo kill the program<br \/>\n<\/h4>\n<p>\nIf you need to kill your program running in the background you can use this\n<\/p>\n<pre>\r\n<code>\r\nsudo killall my_project.a\r\n<\/code><\/pre>\n<h5>\nIf the program has auto run but is&nbsp;in a loop and you can&#39;t kill it!<br \/>\n<\/h5>\n<p>\nOften the keyboard interface to the RPi is still working even though you may not see what you type due to your&nbsp;program writing to the screen in the background. &nbsp;Try typing your username and then password as you normally would at login and then the &quot;sudo&nbsp;killall&quot; above and it may well kill it\n<\/p>\n<h4>\nRunning multiple things and also programs as a&nbsp;non root user<br \/>\n<\/h4>\n<p>\nYou can use brackets around multiple commands each separated&nbsp;by a &#39;;&#39; followed by the &#39;&amp;&#39; to run the set of commands all in the background.\n<\/p>\n<p>\nIf you use the &#39;&amp;&#39; you can also run multiple lines as each is run in the background.\n<\/p>\n<p>\nFinally, by default rc.local runs as the root user. &nbsp;You can change to a different user using su&nbsp;&#8211; USERNAME -c before the command and surrounding it with quotes.\n<\/p>\n<p>\nIn the example below the following occurs:\n<\/p>\n<p style=\"margin-left: 40px;\">\nAfter a 2 second pause my_project.a us run in the background.\n<\/p>\n<p style=\"margin-left: 40px;\">\nAfter a 5 second delay VLC&nbsp;is started to stream the raspberry pi video camera. &nbsp;VLC won&#39;t run as the root user so su -c is used to make it run as the user &quot;pi&quot;\n<\/p>\n<pre>\r\n<code>\r\n#Auto run our application\r\n(sleep 2;sudo \/home\/pi\/projects\/my_project\/my_project.a)&amp;\r\n#Auto run VLC video streaming\r\n(sleep 5;su - pi -c &quot;raspivid -o - -t 0 -n -w 640 -h 480 -fps 15 | cvlc -vvv stream:\/\/\/dev\/stdin --sout &#39;#rtp{sdp=rtsp:\/\/:8554\/}&#39; :demux=h264&quot;)&amp;\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Auto running applications in the GUI See here Auto running terminal applications (non GUI) First ensure your program is executable&nbsp;by finding it in the file manager. &nbsp;Right click on the file and select properties.&nbsp; Select the permissions tab, check the &lsquo;Make the file executable&rsquo; box and press OK. &nbsp;Or&nbsp;from the command line use: sudo chmod [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[187,11],"tags":[],"class_list":["post-678","post","type-post","status-publish","format-standard","hentry","category-application-control","category-raspbian"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=678"}],"version-history":[{"count":25,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/678\/revisions"}],"predecessor-version":[{"id":3730,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/678\/revisions\/3730"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}