How to convert my program into a service?
Tom H
tomh0665 at gmail.com
Fri Apr 2 15:18:57 UTC 2021
On Thu, Apr 1, 2021 at 11:39 PM Bo Berglund <bo.berglund at gmail.com>
wrote:
> 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
For systemd to integrate the change.
> 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!
Good. You're welcome.
> BTW:
> What is the /run/ dir used for? I have never heard about that
> before...
It's a tmpfs that's mounted very early in the boot sequence to hold
runtime data. It used to be "/var/run", which is now a symlink to
"/run".
sshd, for example, creates a file and a directory in "/run". The file
contains the daemon's PID and the directory's used by sshd to chroot
itself as it starts up.
systemd also uses this directory. "/run/systemd/generator/-.mount" is
the system's "/" mount unit.
More information about the ubuntu-users
mailing list