How to convert my program into a service?

Bo Berglund bo.berglund at gmail.com
Tue Mar 30 16:32:16 UTC 2021


On Tue, 30 Mar 2021 12:39:45 +0100, Colin Watson <cjwatson at ubuntu.com> wrote:

>On Tue, Mar 30, 2021 at 12:35:58PM +0200, Bo Berglund wrote:
>> Additional question:
>> If the program is running as the ssytemd service and it needs to be updated, can
>> I then do:
>> 1) Replace the executable file in place (while it is running)
>> 2) Command systemd to restart the service
>> 
>> I have noted that Linux does not protest if I remove a program when it is
>> running so I assume unlike Windows Linux loads the full program into memory at
>> start and does not need the file anymore?
>
>Yes, this is a routine operation on Unix and should work fine.
>

I tested it as follows:
- started the program from the terminal prompt
- connected a TCP client to it
- compiled a new version of the program
- removed the program file
- copied the newly compiled file into the same place
- disconnected the client (normal handshake and logging)
- stopped the program using Ctrl-C in the terminal, normal exit
- immediately started it again, all is OK

So it really proves what you state and what I guessed would work!

So to remotely handle this update I could add a TCP command that would receive a
new program binary and replace the one running now with that.
Then on client disconnect, it would restart itself when it is next idle and not
running a scheduled command.

I have to figure out how to exit and restart one self, though.
How is it handled by systemd, if the service exits will it be restarted?

When I google this I get to numerous "solutions" which reference "if the service
fails" as the exit cause. But I want it to restart (after some seconds) if it
closes normally too (by itself).
In fact it should run unless I have issued a stop command via systemd for the
service, then it should not be started again.
What are the unit parameters for that?

I have seen these:

[Service]  
Restart=always  
StartLimitInterval=90  
StartLimitBurst=3

But this might restart also if I stopped it via systemd?


[Service]
Restart=always
RestartSec=3

Again "always"


[Service]
Type=simple
Restart=on-failure
RestartSec=10
KillMode=process

This example mentions on-failure, but how does systemd know that?
And there is a Killmode setting as well....

Confused....


-- 
Bo Berglund
Developer in Sweden





More information about the ubuntu-users mailing list