How to convert my program into a service?

Bo Berglund bo.berglund at gmail.com
Thu Apr 1 21:37:57 UTC 2021


On Thu, 1 Apr 2021 11:30:36 +0200, Tom H <tomh0665 at gmail.com> wrote:

>On Tue, Mar 30, 2021 at 10:06 AM Bo Berglund <bo.berglund at gmail.com>
>wrote:
>>
>> I have ported a Windows service application originally created
>> using Borland Delphi using its template TService. Right now this
>> ported version is a simple command line program written with Free
>> Pascal and it works as intended if started manually from a
>> terminal.
>>
>> Now I want to convert it into a Linux service under systemd, but I
>> am confused by reading what I find when googling...
>>
>> Is there a authoritative howto document which describes how this
>> should be done including where to put which files?
>>
>> I want the service to be run under a specific user account so that
>> the data files it produces will be accessible by regular users
>> belonging to the same group as the service user.
>>
>> The application is a control server for external equipment and it
>> can be interacted with through a proprietary TCP/IP socket
>> interface. It uses sockets and serial ports for its operation. (I
>> don't know if this is important but I add it here just in case).
>>
>> If additional info is needed I can provide it.
>
>Create "/etc/systemd/system/your_daemon.service" (most basically!) with
>
>[Unit]
>Description=your_daemon
>After=
>Requires=
>
>[Service]
>Type=simple|forking ## there are other types ...
>PIDFile= ## if Type=forking
>ExecStart=/usr/local/bin/your_daemon [options]
>User=your_daemon_user
>
>[Install]
>WantedBy=multi-user.target
>
>If you need a file or directory created under "/run", create a file
>under "/etc/tmpfiles.d" for it.

My service file now looks like this (I did not have User= before):

[Unit]
Description=SSRemote Server
Wants=network.target
After=syslog.target network-online.target

[Service]
Type=simple
ExecStart=/agi/ssremote/bin/SSRemoteServerLx
Restart=always
RestartSec=10
KillMode=process
User=pi

[Install]
WantedBy=multi-user.target

Previously the logfiles the application writes into /agi/ssremote/log were owned
by root.
After I added the User= line in the service file systemd suggested to run:
sudo systemctl daemon-reload

when I stopped the service to restart it with the new setting, so I did.

Then after starting the service again the log files are now owned by user pi.

Thanks for pointing this out!

BTW:
What is the /run/ dir used for? I have never heard about that before...


-- 
Bo Berglund
Developer in Sweden





More information about the ubuntu-users mailing list