{"id":1464,"date":"2022-07-26T04:30:56","date_gmt":"2022-07-26T07:30:56","guid":{"rendered":"https:\/\/blog.abratel.com.br\/?p=1464"},"modified":"2022-07-26T04:35:36","modified_gmt":"2022-07-26T07:35:36","slug":"how-to-run-graphical-program-automatically-boot-raspberry","status":"publish","type":"post","link":"https:\/\/blog.abratel.com.br\/?p=1464","title":{"rendered":"How to run the graphical program automatically boot raspberry"},"content":{"rendered":"<p>If you need access to elements from the X Window System (e.g. you are making a graphical dashboard or game), then you will need to wait for the X server to finish initializing before running your code. One way to accomplish this is to use the&nbsp;autostart&nbsp;system.<\/p>\n<p><strong>Note:<\/strong>&nbsp;Raspbian is based on the LXDE desktop environment. As a result, the location of the autostart script might be different depending on your particular Linux computer and distribution version.<\/p>\n<p>After your desktop environment starts (LXDE-pi, in this case), it runs whatever commands it finds in the profile&#8217;s&nbsp;<em>autostart<\/em>&nbsp;script, which is located at&nbsp;<em>\/home\/pi\/.config\/lxsession\/LXDE-pi\/autostart<\/em>&nbsp;for our Raspberry Pi. Note that the directory&nbsp;<em>pi<\/em>&nbsp;might be different if you created a new user for your Raspberry Pi. If no user&nbsp;<em>autostart<\/em>&nbsp;script is found, Linux will run the global&nbsp;<em>\/etc\/xdg\/lxsession\/LXDE-pi\/autostart<\/em>&nbsp;script instead.<\/p>\n<p>In addition to running commands in&nbsp;<em>autostart<\/em>, Linux will also look for and execute&nbsp;<em>.desktop<\/em>&nbsp;scripts found in&nbsp;<em>\/home\/pi\/.config\/autostart<\/em>. The easiest way to execute GUI programs on boot is to create one of these .desktop scripts.<\/p>\n<h3>Create a .desktop File<\/h3>\n<p>You do not need root-level access to modify your profile&#8217;s (user&#8217;s) autostart and .desktop files. In fact, it is recommended that you do&nbsp;<strong>not<\/strong>&nbsp;use&nbsp;sudo, as you may affect the permissions of the file (e.g. the file would be owned by root) and make them unable to be executed by autostart (which has user-level permissions).<\/p>\n<p>Open a terminal, and execute the following commands to create an&nbsp;<em>autostart<\/em>&nbsp;directory (if one does not already exist) and edit a .desktop file for our clock example:<\/p>\n<p>COPY CODEmkdir \/home\/pi\/.config\/autostart<\/p>\n<p>nano \/home\/pi\/.config\/autostart\/clock.desktop<\/p>\n<p>Copy in the following text into the&nbsp;<em>clock.desktop<\/em>&nbsp;file. Feel free to change the&nbsp;<em>Name<\/em>&nbsp;and&nbsp;<em>Exec<\/em>&nbsp;variables to your particular application.<\/p>\n<p>COPY CODE[Desktop Entry]<\/p>\n<p>Type<strong>=<\/strong>Application<\/p>\n<p>Name<strong>=<\/strong>Clock<\/p>\n<p>Exec<strong>=<\/strong>\/usr\/bin\/python3 \/home\/pi\/clock.py<\/p>\n<p><strong>Note:<\/strong>&nbsp;We are calling&nbsp;<em>python3<\/em>&nbsp;explicitly here to prevent any confusion about which Python version to use.<\/p>\n<p><img loading=\"lazy\" width=\"661\" height=\"392\" src=\"https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2022\/07\/word-image-2.png\" class=\"wp-image-1465\" srcset=\"https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2022\/07\/word-image-2.png 661w, https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2022\/07\/word-image-2-300x178.png 300w\" sizes=\"(max-width: 661px) 100vw, 661px\" \/><\/p>\n<p>Save and exit with&nbsp;<em>ctrl<\/em>&nbsp;+&nbsp;<em>x<\/em>, followed by&nbsp;<em>y<\/em>&nbsp;when prompted to save, and then&nbsp;<em>enter<\/em>. Reboot with:<\/p>\n<p>COPY CODEsudo reboot<\/p>\n<p>When your Raspberry Pi starts up, make sure to log in to the desktop (if it does not do so automatically). Your script should start running right away!<\/p>\n<h3>Troubleshooting<\/h3>\n<h4>Nothing Happens<\/h4>\n<p>If your script does not run as soon as you see the desktop, there could be several issues. First, make sure that you have logged in (autostart does not start until you have logged in). You could also try enabling auto-login in&nbsp;raspi-config. Second, make sure you are using absolute directory names (e.g.&nbsp;\/home\/pi\/clock.py). Third, try following some of the suggestions below to enable&nbsp;<em>stdout<\/em>&nbsp;and&nbsp;<em>stderr<\/em>&nbsp;to see what&#8217;s going on.<\/p>\n<h4>Use a Specific Version of Python<\/h4>\n<p>As it turns out,&nbsp;<em>autostart<\/em>&nbsp;runs before .bashrc, so the command&nbsp;python&nbsp;still refers to Python 2. To explicitly call Python 3, we should change our command in&nbsp;<em>autostart<\/em>&nbsp;to:<\/p>\n<p>COPY CODE@\/usr\/bin\/python3 \/home\/pi\/clock.py<\/p>\n<h4>Debugging<\/h4>\n<p>Unfortunately, running a program from autostart makes it difficult to output or log to a file, and lxterminal (the default terminal program in Raspbian) is too simplistic to help us here. To get some kind of logging, we&#8217;ll need to use a different terminal program (we&#8217;ll use xterm). Open a terminal and enter the following command<\/p>\n<p>COPY CODEsudo apt-get install xterm -y<\/p>\n<p>In your .desktop file, change your&nbsp;Exec&nbsp;command to the following:<\/p>\n<p>COPY CODEExec<strong>=<\/strong>xterm <strong>&#8211;<\/strong>hold <strong>&#8211;<\/strong>e &#8216;\/usr\/bin\/python3 \/home\/pi\/clock.py&#8217;<\/p>\n<p>Restart your Raspberry Pi. Now, after you log into your desktop, you should see a new terminal window open followed by your program running. If you stop your program (exiting out of it, pressing&nbsp;<em>ctrl<\/em>&nbsp;+&nbsp;<em>c<\/em>&nbsp;in the xterm window, or killing the process as detailed below), the xterm window will stay open, allowing you to read all of the output and error statments from your program.<\/p>\n<h4>How to Stop Your Program<\/h4>\n<p>If your program is running in the background, there might be no obvious way of halting it. You can always delete your .desktop files and restart, but that might take a while. A better option might be to kill the process associated with your program. In a terminal, enter the following:<\/p>\n<p>COPY CODEsudo ps -ax | grep python<\/p>\n<p>ps -ax&nbsp;tells Linux to list out all the currently processes. We send that output to&nbsp;grep, which allows us to search for keywords. Here, we&#8217;re looking for&nbsp;<em>python<\/em>, but feel free to change it to the name of your program. Find the process ID (PID) number to the left of the listed process, and use the&nbsp;kill&nbsp;command to terminate that process:<\/p>\n<p>COPY CODEsudo kill <strong>&lt;<\/strong>PID<strong>&gt;<\/strong><\/p>\n<p><strong>Heads up!<\/strong>&nbsp;Make sure you type the PID correctly! If you kill the wrong process, you could halt Linux, and you would need to reboot again.<\/p>\n<p><img loading=\"lazy\" width=\"600\" height=\"378\" src=\"https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2022\/07\/word-image-3.png\" class=\"wp-image-1466\" srcset=\"https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2022\/07\/word-image-3.png 600w, https:\/\/blog.abratel.com.br\/wp-content\/uploads\/2022\/07\/word-image-3-300x189.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>If you are using the&nbsp;<em>clock.py<\/em>&nbsp;example, you should see the application quit out.<\/p>\n<h4>How to Stop Your Program from Running on Boot<\/h4>\n<p>To prevent your program from running on boot, you just need to delete the .desktop file. In a terminal, enter the command (replacing&nbsp;<em>clock.desktop<\/em>&nbsp;with the name of your particular .desktop file):<\/p>\n<p>COPY CODErm \/home\/pi\/.config\/autostart\/clock.desktop<\/p>\n<p>Reboot your Pi, and your program should no longer run on startup.<\/p>\n<p>FreeOffice TextMaker lets you create attractive documents of any size in no time. To help you get started, we have summarized some of the most important functions in this document. You can also use this document to record your own notes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you need access to elements from the X Window System (e.g. you are making a graphical dashboard or game), then you will need to wait for the X server to finish initializing before running your code. One way to accomplish this is to use&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1490,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/1464"}],"collection":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1464"}],"version-history":[{"count":2,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/1464\/revisions"}],"predecessor-version":[{"id":1491,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/posts\/1464\/revisions\/1491"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=\/wp\/v2\/media\/1490"}],"wp:attachment":[{"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.abratel.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}