Start at Boot
To start the Sprocket software at boot, we must create a Unix service using the systemd System and Service Manager.
Create a Launch Script
The launch script will reside in the user home directory and will launch a named screen session and then launch the Sprocket Python code.
-
In an SSH session, navigate to the user home directory using
cd ~
. -
In your favorite text editor, create a file named start_sprocket.sh with the following contents:
#!/bin/sh screen -dm -S sprocket python ~/Sprocket/main.py
-
Set the start_sprocket.sh script as executable using the
chmod
command:chmod +x start_sprocket.sh
-
Test that the script works by launching it.
./start_sprocket.sh
Create a Systemd service
Now that we have our launch script, create a systemd service.
-
Navigate to the systemd system directory
/lib/systemd/system
cd /lib/systemd/system
-
In your favorite text editor, as sudo, create a file named sprocket.service with the following contents:
[Unit] Description=Run Sprocket on boot After=multi-user.target [Service] ExecStart = /home/pi/start_sprocket.sh RemainAfterExit=yes User=pi [Install] WantedBy=multi-user.target
-
Reload the systemctl daemon.
sudo systemctl daemon-reload
-
Register the Sprocket service.
sudo systemctl enable sprocket.service
-
Check that the service is enabled.
sudo systemctl list-unit-files | grep sprocket
-
Manually start the service.
sudo systemctl start sprocket.service
Now, the Sprocket code will automatically start whenever the system is rebooted. To connect to the screen session to interface the program, use the screen -r
command.
Debugging Systemd service
To check the status of the service, use the sudo systemctl status sprocket.service
command.