For the Sound and Vibration Measurement Hat for Raspberry Pi road test, I'm reviewing Measurement Computing's IEPE Measurement DAQ HAT for Raspberry Pi. |
I have done this before a few years ago, for another Pi SCPI instrument: a programmable LAB 8 channel switcher. The screen captures in this post show that the services are executed by root, but if you follow the instructions, it 'll be the pi user that runs them.
Prerequisites
You should have built the two binaries for this project.
daqhats_scpi_socket
daqhats_service
These binaries should be placed in /home/pi/bin
, and made executable (chmod +x <binary_name>
). If you place them in another location, amend the scripts below.
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>
).
daqhats_start.sh
#!/bin/bash echo "start daqhats instrument service. Wait 5 seconds to complete ..." /home/pi/bin/daqhats_service 2501 2502 0 & sleep 5 echo "start daqhats scpi service." /home/pi/bin/daqhats_scpi_socket 2500 2501 & echo "start sequence completed"
daqhats_stop.sh
#!/bin/bash killall -9 "/home/pi/bin/daqhats_scpi_socket" killall -9 "/home/pi/bin/daqhats_service"
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 instrument service's binary to start its socket listening thread. Both scripts will be invoked by the service handler to start and stop the daemon.
Service Script
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 Raspberry Pi OS' 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 daqhats.service
in the same directory.
[Unit] Description=daqhats 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/daqhats_start.sh ExecStop=/home/pi/bin/daqhats_stop.sh RemainAfterExit=true TimeoutStartSec=5min User=pi
Create a symbolic link for systemd
:
sudo chmod 644 ./daqhats.service
sudo ln -s /home/pi/bin/daqhats.service /lib/systemd/system/daqhats.service
Then, tell systemd
that there's a new configuration and tell it to enable the service:
sudo systemctl daemon-reload
sudo systemctl enable daqhats.service
If you reboot, the raspberry will start with the daqhats Service running.
Check:
Managing the Service
You can use the usual systemd
commands to verify, stop and start the daemon.
sudo systemctl status daqhats.service
sudo systemctl stop daqhats.service
sudo systemctl start daqhats.service
sudo systemctl restart daqhats.service