I'm building a SCPI electronics lab instrument for Linux.
In this post I'm creating a service wrapper and install the Linux service.
It will start at Pi startup, and can be stopped / started as you'd expect from a service.
You should have built the two binaries for this project, as documented in post 4a and 4b. If you didn't, the binaries are available as archive attached to post 4b.
These binaries should be placed in /home/pi/bin, and made executable (chmod +x <binary_name>).
If you're using a recent raspbian (I use buster lite) there are no other dependencies.
Start and Stop Shell Scripts
I've created a startup and a shutdown shell script in that same /home/pi/bin directory, and made them executable (again using chmod +x <shell_script_name>).
#!/bin/bash echo "start piface. Wait 5 seconds to complete ..." /home/pi/bin/linux_piface_digital_service_cpp 2223 & sleep 5 echo "start scpi." /home/pi/bin/linux_scpi_socket 2222 2223 & echo "start sequence completed"
#!/bin/bash killall -9 "/home/pi/bin/linux_scpi_socket" killall -9 "/home/pi/bin/linux_piface_digital_service_cpp"
These scripts take care that the right order is used to start and terminate both executables.
There's a 5 second sleep to allow the piface binary to start its socket listening thread.
Both scripts will be invoked by the service handler to start and stop the daemon.
Now the core of this post: creating that Linux service that starts up at boot, and can be restarted by the user.
I've used the raspbian networking.service as inspiration, and this website for the instructions: https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/
Create a new service wrapper in /lib/systemd/system, called scpi_piface.service.
sudo nano /lib/systemd/system/scpi_piface.service
Then enter this content:
[Unit] Description=PiFace SCPI Service DefaultDependencies=no Requires=ifupdown-pre.service Wants=network.target After=local-fs.target network-pre.target apparmor.service systemd-sysctl.service systemd-modules-load.service ifupdown-pre.service network.target Before=shutdown.target Conflicts=shutdown.target [Install] WantedBy=multi-user.target WantedBy=network-online.target [Service] Type=oneshot #EnvironmentFile=-/etc/default/networking ExecStart=/home/pi/bin/scpi_face_start.sh ExecStop=/home/pi/bin/scpi_face_stop.sh RemainAfterExit=true TimeoutStartSec=5min #User=pi
I've chosen to wait for the full network stack being up.
You have to mod the file attributes:
sudo chmod 644 /lib/systemd/system/scpi_piface.service
Then, tell systemd that there's a new configuration and tell it to enable the service:
sudo systemctl daemon-reload sudo systemctl enable scpi_piface.service
If you reboot, the raspberry will start with the PiFace SCPI Service running.
Managing the Service
You can use the usual systemd commands to verify, stop and start the daemon.
sudo systemctl status scpi_piface.service sudo systemctl stop scpi_piface.service sudo systemctl start scpi_piface.service sudo systemctl restart scpi_piface.service