In a recent project, we had to use a raspberry pi as an edge controller for remote monitoring an Industrial shilling machine.

Our Software team is more experienced in C# /dotnet core than C/C++ for linux, so they built the application using latest dotnet core 2.2, and had it working fine, now it was my turn to test and make it run as a service and start if system reboot due to power loose or any other resone.

Step One; go to this directory in your RPi

$ cd /etc/systemd/system

Step two; issue this command to create new file and open editor:

$ sudo nano serviceName.service // this create a ​file with the name of your service

Step three; type the following lines in the file.

[Unit]
Description=BSCC service

[Service]
ExecStart=/bin/dotnet/dotnet /home/pi/BSCC/RaspberryPi.dll
WorkingDirectory=/home/pi/BSCC/
User=root
Group=root
Restart=on-failure
SyslogIdentifier=dscc-service
PrivateTmp=true
Type=idle

[Install]
WantedBy=multi-user.target

after that save the file, if you are using nana editor like me use the following command

Ctrl+X.  then answer yes to the save quastion

Now having the service ready we need to call the following commands in sequence, it may need the use of sudo

$ sudo systemctl daemon-reload  
$ sudo systemctl enable serviceName.service
$ sudo systemctl start serviceName.service

after this point your service is running, to check on it or do any changes you can use any of the following commands:

to check status:
$ sudo systemctl status serviceName.service
to start :  
$ sudo systemctl start serviceName.service
to stop:  
$ sudo systemctl stop serviceName.service
to restart:  
$ sudo systemctl restart serviceName.service

for debugging purposes and following up on any log messages you application write you can use this command.

journalctl --unit serviceName.service --follow 

References:

To know more about units and services in linux check this link & this link too


Leo Idraikh

Leo Idraikh

Electronics Engineer by profession learned mechanical design and Industrial design out of passion with a sparkle of programming skills in different languages

Leave a Reply

%d bloggers like this: