You are not logged in.

#1 2020-08-26 19:56:27

kpeter
Member
Registered: 2020-08-26
Posts: 3

Compiled executable as systemd service not working

Hello,

I have an infinite compiled CPP program that has to be started as a service. I tried the following config, but it produces nothing:

[Unit]
Description=spicli-server

[Service]
ExecStart=/root/a.out

[Install]
WantedBy=multi-user.target

I enabled the service and started it.
This is what I get:

[root@osboxes ~]# systemctl start sserver2.service
[root@osboxes ~]# systemctl status s-server2.service
● s-server2.service - s-server2
     Loaded: loaded (/etc/systemd/system/s-server2.service; enabled; vendor preset: disabled)
     Active: inactive (dead) since Wed 2020-08-26 15:54:00 EDT; 5s ago
    Process: 622 ExecStart=/root/a.out (code=exited, status=0/SUCCESS)
   Main PID: 622 (code=exited, status=0/SUCCESS)

Aug 26 15:54:00 osboxes systemd[1]: Started s-server2.
Aug 26 15:54:00 osboxes systemd[1]: s-server2.service: Succeeded.

This is a test program creating a file, but as a service, it does nothing. When I run it manually, everything is fine. Could you please help me?

Offline

#2 2020-08-26 20:48:20

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,371

Re: Compiled executable as systemd service not working

It runs and exits successfully. What are you actually trying to do here, can you share the code?

Offline

#3 2020-08-26 21:21:30

kpeter
Member
Registered: 2020-08-26
Posts: 3

Re: Compiled executable as systemd service not working

This is the code, really simple. I can check its result if the file is created. When I run manually, there is no problem, but the service seems not doing anything. Everybody can execute this file. The final program to start is much more complicated, I just wanted to use it as test, but it failed sad This is a simple program, there is no magic with daemons and etc, as it can be read on other forums. Am I doing something wrong? I do not know Linux well.

#include <stdio.h>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
        std::stringstream logFileName;
        logFileName << "_log.txt";
        std::string logFileNameS = logFileName.str();
        std::ofstream logFile;
        logFile.open (logFileNameS);

        logFile << " ----- SERVER STARTED. -----" << std::endl;


        logFile.close();
    return (0);
}

Last edited by kpeter (2020-08-26 21:22:51)

Offline

#4 2020-08-26 21:47:39

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,371

Re: Compiled executable as systemd service not working

Where are you looking for the file? The working directory for a system instance of systemd is / if not specified otherwise.

FWIW seeing the hostname I don't think this is an actual Arch install, and you will probably have a generally better experience if you opt for one of the more beginner friendly distributions. Maybe read the man pages on systemd units and exec environments and so fort to have a general grasp of how this works.

Last edited by V1del (2020-08-26 21:53:18)

Offline

#5 2020-08-26 23:23:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,517
Website

Re: Compiled executable as systemd service not working

Or just don't use relative file names in executables meant to run as services.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2020-08-27 00:56:26

kpeter
Member
Registered: 2020-08-26
Posts: 3

Re: Compiled executable as systemd service not working

Yes, that was the wrong path I was looking at. I do not know why I was getting errors first telling that executable is not fount under /usr/bin, now this is just working, and now I can find the file. This is an Arch install, but from OSBoxes.org, I do not like it, but it was a fast option to start with, however, it is slower than a copy installed by me. Thank you for your fast help.

Offline

Board footer

Powered by FluxBB