{"id":2318,"date":"2015-11-04T19:55:47","date_gmt":"2015-11-04T19:55:47","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2318"},"modified":"2024-11-27T16:24:57","modified_gmt":"2024-11-27T16:24:57","slug":"auto-running-programs-gui","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/pi-operating-systems\/raspbian\/auto-running-programs-gui","title":{"rendered":"Auto Running Programs-GUI"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Screensaver<\/h4>\n\n\n\n<p>If you need to disable the screen saver \/ screen auto switch off see <a href=\"https:\/\/raspberry-projects.com\/pi\/pi-operating-systems\/raspbian\/screensaver\">here<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Auto-running a Python file within a terminal window<\/h4>\n\n\n\n<p>Edit the X Windows autostart file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/xdg\/lxsession\/LXDE-pi\/autostart<\/code><\/pre>\n\n\n\n<p>Add the following line to the end:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@lxterminal -e python \/home\/pi\/myprogram.py<\/code><\/pre>\n\n\n\n<p>So you end up with something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@lxpanel --profile LXDE-pi\n@pcmanfm --desktop --profile LXDE-pi\n@xscreensaver -no-splash\n@lxterminal -e python \/home\/pi\/myprogram.py<\/code><\/pre>\n\n\n\n<p>That\u2019s it, reboot and you should see a terminal window automatically open and run your program.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Auto-running a bash script within a terminal window<\/h4>\n\n\n\n<p>Edit the X Windows autostart file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/xdg\/lxsession\/LXDE-pi\/autostart<\/code><\/pre>\n\n\n\n<p>Add the following line to the end:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@lxterminal -e \/home\/pi\/myscript.sh<\/code><\/pre>\n\n\n\n<p>So you end up with something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@lxpanel --profile LXDE-pi\n@pcmanfm --desktop --profile LXDE-pi\n@xscreensaver -no-splash\n@lxterminal -e \/home\/pi\/myscript.sh<\/code><\/pre>\n\n\n\n<p>Make sure your script is executable (right click &gt; Properties &gt; Permissions).<br>That\u2019s it, reboot and you should see a terminal window automatically open and run your program.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Auto running a program with auto restart on crash<\/h4>\n\n\n\n<p>You can use a simple bash script that runs the program and re-runs it should it crash, with writing restart events to a log file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/sh\n\nCOMMAND=\"python .\/home\/myfolder\/my_application.py\"\nLOGFILE=\"\/home\/myfolder\/restartslog.txt\"\n\nwritelog () {\n  now=`date`\n  echo \"$now $*\" &gt;&gt; $LOGFILE\n}\n\nwritelog \"Starting up\"\nwhile true ; do\n  $COMMAND\n  writelog \"Exited with status $?\"\n  sleep 5      #Pause before restart (optional)\n  writelog \"Auto restarting app\"\ndone<\/code><\/pre>\n\n\n\n<p>Make the .sh file executable and then run it. To forcibly exit use: CTRL+C<\/p>\n\n\n\n<p>Auto run the bash script as detailed above on this page<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">If you program is python that needs to run inside its virtual environment<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#Instead of this:\nCOMMAND=\"python .\/home\/myfolder\/my_application.py\"\n\n#Use this:\nCOMMAND=\"\/home\/myfolder\/venv\/bin\/python3 \/home\/myfolder\/venv\/app\/my_application.py\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Run with a delay before launching program<\/h4>\n\n\n\n<p>To run with a delay before your app launches, instead of adding the path to your app, provide a path to a script we will create:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@sh \/home\/pi\/myapp_start.sh<\/code><\/pre>\n\n\n\n<p>Exit and save, then create the script file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/home\/pi\/myapp_start.sh<\/code><\/pre>\n\n\n\n<p>Insert the following into the file, setting the delay you want in seconds and the path to your app executable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/sh\nsleep 4\n\/home\/pi\/projects\/myfolder\/myapp<\/code><\/pre>\n\n\n\n<p>Exit and save, then finally make the script executable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chmod +x \/home\/pi\/myapp_start.sh<\/code><\/pre>\n\n\n\n<p>That&#8217;s it, reboot and your app should launch after the delay period<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Killing the application while running<\/h4>\n\n\n\n<p>Plug in a USB keyboard if necessary and press ALT+F4<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Screensaver If you need to disable the screen saver \/ screen auto switch off see here. Auto-running a Python file within a terminal window Edit the X Windows autostart file: Add the following line to the end: So you end up with something like this: That\u2019s it, reboot and you should see a terminal window [&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-2318","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\/2318","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=2318"}],"version-history":[{"count":19,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2318\/revisions"}],"predecessor-version":[{"id":3815,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2318\/revisions\/3815"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}