0

I currently have an executable file, which is the program I am developping, in C. It loads a configuration file - depending on which it will have a certain behaviour. This was tested, and works when I launch it from a shell.

However I want it to be started with the system, and as such I made a service for it. This service simply launches my executable on startup.

When doing so, the program will always have the default behaviour, as if the file was ignored.

Is there a reason why this would happen? My current guess is that the program could be starting while the files aren't ready to be accessed for some reason (as my program writes on the framebuffer, as intended, long before an user can log into the system via serial ports).

I have no idea if this is correct, and how to fix it anyway. Thanks in advance.

10
  • How is the path to the file specified? Commented Dec 16, 2019 at 9:40
  • @muru it directly opens the name of the file as it is in the same folder as the executable Commented Dec 16, 2019 at 9:42
  • And have you set the working directory to that folder in the service definition? Commented Dec 16, 2019 at 9:44
  • It launches the executable with a given path yes Commented Dec 16, 2019 at 9:56
  • 1
    That's not what I asked. Commented Dec 16, 2019 at 9:59

1 Answer 1

1

Is the executable file looking for the configuration file in the current working directory? If so, then you should be aware that any services started at boot time will usually have their working directory set to / unless the service specifically says otherwise.

In a .service file, you could use a line like

WorkingDirectory=/home/user/demo 

to start the service process already cd'd to the desired directory. See man systemd.exec for more details.

Or is the executable file trying to use the value of the HOME environment variable as part of the pathname of the configuration file? Services don't necessarily have the HOME environment variable set at all. If necessary, you could set it in a .service file with a line like:

Environment="HOME=/home/user" 

However, this is somewhat unexpected requirement for a program that is supposed to be runnable as a systemd service. You might want to use some other environment variable instead of HOME, and document it explicitly.

Within the program, you might want to specify the location of the configuration file as a full pathname, if you aren't already doing so.

3
  • That's interesting to know... The binary is indeed in a subdirectory of home, and uses multiple files there. The services does however have a pathname to work with: ExecStart=/home/user/demo should I modify everything in my program to have the full pathname, or is there a way to give the service this environment variable? Commented Dec 16, 2019 at 9:59
  • The service can tell where its executable is by looking at /proc/{pid}/self and infer the path to the config file from this. But IMHO if you want to make a service, the config location should be in /etc, or at least pass the config path as an argument. Commented Dec 16, 2019 at 10:48
  • I edited my answer to include more clear examples of how to set the working directory and/or environment variables for the service. Commented Dec 16, 2019 at 11:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.