Skip to content

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.

  1. In an SSH session, navigate to the user home directory using cd ~.

  2. 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
    

  3. Set the start_sprocket.sh script as executable using the chmod command:

    chmod +x start_sprocket.sh
    

  4. 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.

  1. Navigate to the systemd system directory /lib/systemd/system

    cd /lib/systemd/system
    

  2. 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
    

  3. Reload the systemctl daemon.

    sudo systemctl daemon-reload
    

  4. Register the Sprocket service.

    sudo systemctl enable sprocket.service
    

  5. Check that the service is enabled.

    sudo systemctl list-unit-files | grep sprocket
    

  6. 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.