How to Execute a Script at Startup on the Raspberry Pi
The Raspberry Pi computer is a great tool for creating all kinds of interesting DIY projects and computer-powered builds. Whether it be used as a media center, web server, gaming console, or simply a computer, there are countless reasons why you might need a particular program or script to automatically startup when the Raspberry Pi desktop loads. While there are as many ways to run scripts at startup as there are uses for the Raspberry Pi, executing a script when the desktop environment boots is the easiest method with the most versatility.
Water.org is an international nonprofit organization that has positively transformed millions of lives around the world through access to safe water and sanitation. Founded by Gary White and Matt Damon, Water.org pioneers innovative, market-driven solutions to the global water crisis — breaking down barriers to give families hope, health and the opportunity to break the cycle of poverty.
Click below to let us know you read this article, and wikiHow will donate to Water.org on your behalf. Thanks for helping us achieve our mission of helping people learn how to do anything.
Configure your Raspberry Pi
- 1Understand the requirements and how it works. This method requires the PIXEL desktop environment with the latest version of the Raspbian Jessie operating system. It is achieved by editing an "autostart" file that runs commands at startup in the LXDE desktop environment, which is the framework the Raspberry Pi's PIXEL desktop is built on. This method is preferred in scenarios where:
- your script requires the desktop environment to run
- your script needs to run from a terminal window
- you want to specify which users the script will autostart for
- 2Ensure your Raspberry Pi boots to the desktop. Click on the raspberry icon in the upper left side of the task bar. Then go to Preferences and click Raspberry Pi Configuration.
- 3Click on the System tab. In the "Boot" section, make sure the option "To Desktop" is selected. Then click OK. Great, let's get started!
Part2
Editing the Autostart File
- 1Open the main autostart file in a text editor. In a new terminal window type the following command:sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
- 2Using the arrow keys, navigate to the end of the second line and press ↵ Enter.This will created a new line above the @xscreensaver -no-splash entry. Many users have reported that commands added below this line do not run successfully so it's common practice to add your custom commands above the @xscreensaver entry.
- 3Add a command to launch your script. This is where you have the most options as you can enter any terminal command you would normally use to launch your script. For example, to run a bash script called "myscript.sh" which is located in the home directory of the user "pi" one would enter the command:/home/pi/myscript.sh
- 4Include the full paths to every filename. For example, to run a Python script that requires superuser permissions called "myscript.py" which is located in the home directory of the user "pi", you would enter the full path to both the Python script itself and the Python interpreter, such as:sudo /usr/bin/python /home/pi/myscript.pyLastly, if you wish to run your script in a new terminal window on startup, you can use the @lxterminal command with the -e modifier followed by the full path of your script in quotes. For example:@lxterminal -e "/home/pi/myscript.sh"This will execute the script from a new terminal window.
Part3
Save the Autostart File
- 1When you have entered the proper commands to launch your script, press Ctrl+X. You will be asked if you would like to save your changes, type Y and press ↵ Enter.
- If you don't want your script to run on startup any longer, simply edit the autostart file once again, only this time delete the lines you added previously. Once you save and exit, your script will no longer run automatically on startup.
- 2Test it out! In the terminal type:sudo reboot
Part4
Edit the Autostart File of a Specific User
- 1Edit the autostart file of an individual user. If you have multiple user accounts on your Raspberry Pi and want a script to autostart for only a specific user, you must instead edit the autostart file associated with that specific user. For example, for the default user "pi" the autostart file is located at:/home/pi/.config/lxsession/LXDE-pi/autostartTherefore, to edit this file, in a terminal window you would type:sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
- 2Repeat the previous editing steps. Just like before, input the desired command to launch your script in the correct place in the autostart file. When you are satisfied, be sure to save your changes before you exit.
- 3Test it out. In the terminal type:sudo reboot
wikiHow's mission is to help people learn, and we really hope this article helped you. Now you are helping others, just by visiting wikiHow.
Water.org is an international nonprofit organization that has positively transformed millions of lives around the world through access to safe water and sanitation. Founded by Gary White and Matt Damon, Water.org pioneers innovative, market-driven solutions to the global water crisis — breaking down barriers to give families hope, health and the opportunity to break the cycle of poverty.
Click below to let us know you read this article, and wikiHow will donate to Water.org on your behalf. Thanks for helping us achieve our mission of helping people learn how to do anything.
Comentarios
Publicar un comentario