Bosse_B
Posts: 995
Joined: Thu Jan 30, 2014 9:53 am

Trying to create a systemd service application...

Sun Jun 21, 2020 1:31 pm

I want to create a program for RPi, which will mimic an existing program I wrote for Windows back around 2004.
That program was a service application created using Delphi7 (object pascal language).
But I want to create a clone of it for RPi4 Linux, and the most problematic part is the interface to the operating system, which was provided by the application class I used at the time without knowing how it was accomplished...

What are the requirements for a program, which can be run as a service in Raspbian Buster (systemd)?
AFAICT this is what is needed:
- Command line program, i.e. no GUI part whatsoever
- Shall run "forever" once started

Questions:
1) How does systemd shut the service program down?
By killing it outright, or is there some communication mechanism that I need to implement

2) Is there some system interface I need to implement?

3) Can any kind of command line program be used as a Linux service as long as it does not use a user interface?
Bo Berglund
Sweden

User avatar
RamaSpaceShip
Posts: 60
Joined: Sun Apr 26, 2020 12:19 pm

Re: Trying to create a systemd service application...

Sun Jun 21, 2020 4:04 pm

Bosse_B wrote:
Sun Jun 21, 2020 1:31 pm
I want to create a program for RPi, which will mimic an existing program I wrote for Windows back around 2004.
That program was a service application created using Delphi7 (object pascal language).
But I want to create a clone of it for RPi4 Linux, and the most problematic part is the interface to the operating system, which was provided by the application class I used at the time without knowing how it was accomplished...

What are the requirements for a program, which can be run as a service in Raspbian Buster (systemd)?
AFAICT this is what is needed:
- Command line program, i.e. no GUI part whatsoever
- Shall run "forever" once started
None. Any application can be run as a systemd service, while it is generally more confortable to reference in the service file a bash script which sets up some environment variables, create necessary directories, cd in the right one, and exec the application at its end, passing parameters and setting the IO redirections.
Bosse_B wrote:
Sun Jun 21, 2020 1:31 pm
Questions:
1) How does systemd shut the service program down?
By killing it outright, or is there some communication mechanism that I need to implement
It tries first with kill -TERM to allow the application to terminate gracefully
If that fails, then it uses kill -KILL
Bosse_B wrote:
Sun Jun 21, 2020 1:31 pm
2) Is there some system interface I need to implement?
None.
Bosse_B wrote:
Sun Jun 21, 2020 1:31 pm
3) Can any kind of command line program be used as a Linux service as long as it does not use a user interface?
Any command line program can be used, even with a user interface. It mainly depends on the nature of the user interface.
Some can easily be handled, for some others, it may be difficult or even impossible.
For example, I run Firefox as a systemd service.

Bosse_B
Posts: 995
Joined: Thu Jan 30, 2014 9:53 am

Re: Trying to create a systemd service application...

Mon Jun 22, 2020 4:11 pm

RamaSpaceShip wrote:
Sun Jun 21, 2020 4:04 pm
For example, I run Firefox as a systemd service.
That is a bit weird, does it display a window or is it completely hidden?
But it raises another question:
If I run a GUI application (one with a main form) as a systemd service, does it then act differently than a simple command line program does?
For example does it have more operating system communications capabilities than the simple program?
I mostly program in RAD tools (like Delphi on Windows or FreePascal/Lazarus on Linux and Windows) and then one has little contact with the actual behind the scenes goings on between the OS and the application. So I am pretty ignorant about these things....
Bo Berglund
Sweden

User avatar
RamaSpaceShip
Posts: 60
Joined: Sun Apr 26, 2020 12:19 pm

Re: Trying to create a systemd service application...

Mon Jun 22, 2020 5:41 pm

Bosse_B wrote:
Mon Jun 22, 2020 4:11 pm
RamaSpaceShip wrote:
Sun Jun 21, 2020 4:04 pm
For example, I run Firefox as a systemd service.
That is a bit weird, does it display a window or is it completely hidden?
It displays a window (full screen) and is used as the UI for another service.
This is not weird at all. It provides very flexible opportunities for the UI setup.
Bosse_B wrote:
Mon Jun 22, 2020 4:11 pm
But it raises another question:
If I run a GUI application (one with a main form) as a systemd service, does it then act differently than a simple command line program does?
For example does it have more operating system communications capabilities than the simple program?
I mostly program in RAD tools (like Delphi on Windows or FreePascal/Lazarus on Linux and Windows) and then one has little contact with the actual behind the scenes goings on between the OS and the application. So I am pretty ignorant about these things....
On Linux, the fact that there is a graphical part makes no difference with regard to systemd. You just have to set up the graphical environment together with your app. The communications between the app and the graphical environment is not managed by the operating system.

On Linux, the OS does not embed the graphical environment. This allows for different ones (they are many) depending only on your choice.

Return to “Raspberry Pi OS”