[LINUX] [Easy] Execute any program when Raspberry Pi starts [Shell Script]

Introduction

I will show you how to execute an arbitrary program with a shell script when you start the Raspberry Pi. Also, I tried running the python file with a shell script to see if it was actually executed. Programs other than python can be executed by rewriting the shell.

People who want to read

・ People who want to execute some program when starting Raspberry Pi (or Linux) ・ What is a shell? People or people who are not very familiar with (For the shell, I hope you can read this commentary which can be read in about 3 minutes.)

environment

Hard: Raspberry Pi 3 Model B + (If the OS is the same, I think it can be executed by the following method) OS : Raspbian GNU / Linux 10 (Note that you cannot use the following method on Windows !!)

Implementation

1. Create a shell script file

Create autostart.sh in / usr / local / bin /.

pi@raspberrypi:~ $ sudo nano /usr/local/bin/autostart.sh

This time I want to execute hello.py (described later in 3.) in the / home / pi / directory, so autostart.sh makes the following changes and saves (ctrl + s). If you are worried about whether you can change or save it, enter the ↑ command again.

#!/bin/sh
python3 /home/pi/hello.py

In addition, give execute permission to the shell script.

pi@raspberrypi:~ $ chmod 755 /usr/local/bin/autostart.sh

2. Change rc.local

Make changes to rc.local so that autostart.sh can be run at startup.

The specific changes are as follows. Only the last part of rc.local has been changed)

fi

exit 0

--After change

fi

autostart.sh

exit 0

3. Create a python file

Create hello.py in the / home / pi / directory.

print("hello, ivrc")

4. Reboot and check

You can either use the GUI or sudo reboot from the terminal, so please reboot and check.

in conclusion

This time I did it with Raspberry pi, but if you change the way of writing the shell a little, you can do the same with Unix-like OS other than Raspbian and languages other than Python. I'm also studying Shell, so I'll do my best to write it on my own little by little.

Reference article

I want to execute the script when starting Raspberry Pi --Qiita

Create a shell script with Raspbian

Recommended Posts

[Easy] Execute any program when Raspberry Pi starts [Shell Script]
[Ubuntu] How to execute a shell script