I have developed a Python program called ping_script.py, which runs on a Raspberry Pi and continuously pings a hostname and save the results to text file. For checking purpose, I want to display the ongoing pinging process on a 5″ LCD display when the Raspberry Pi starts up.
To get the job done, I tackle it in two steps: first, set up an automatic launch of a new terminal when the Raspberry Pi boots up, and then run the Python program once the Pi’s terminal is up and running.
Step 1:
First and foremost, I’ll ensure that a fresh terminal opens up automatically upon the Raspberry Pi’s startup. This can be accomplished by utilizing the “lxterminal” command, which serves as the default terminal emulator within the Raspberry Pi OS (previously referred to as Raspbian). Here’s how you can do it:
Open the LXDE autostart configuration file by running the following command in the terminal:
sudo nano ~/.config/lxsession/LXDE-pi/autostart
In the configuration file, scroll down to the bottom and append the following line without any line breaks at the end:
@lxterminal
Save the file by pressing Ctrl + X
, then press Y
to confirm, and finally press Enter to exit.
Step 2:
Next, I open a new terminal and navigate to the home directory of the login user. Then edit the .bashrc file by typing
sudo nano .bashrc
Scroll to the end of the file and add the following lines These lines specifies that the Python program should be executed when the Pi’s terminal is open.
cd /home/user
python3 ping_script.py
Save the changes to the .bashrc and exit the text editor.
Job done:
After rebooting your Raspberry Pi, the LXTerminal should open automatically and execute ping_script.py.