Neo007
Posts: 12
Joined: Mon Nov 28, 2016 3:23 pm

[SOLVED] [Systemctl] Own .service file doesn't work

Wed Nov 30, 2016 10:40 am

Hi everyone some one can help me with my systemd file ?

I've made my own program in C who run like a charm. But I want to launch it systemctl.

Here's my systemd file :

Code: Select all

[Unit]
Description=SensIndoor Logger
After=network.target

[Service]
Type=forking
PrivateTmp=yes
User=root
Group=root
# Run ExecStartPre with root-permissions
PermissionsStartOnly=true
ExecStartPre=-/bin/mkdir /var/run/SensIndoor
ExecStartPre=/bin/chown -R root:root /var/run/SensIndoor

ExecStart=/usr/local/bin/SensIndoor_Logger %I
PIDFile=/var/run/SensIndoor/SensIndoor.pid

[Install]
WantedBy=default.target
And here you can fin the log error:

Code: Select all

root@raspberrypi:~# systemctl status SensIndoor@ttyACM0.service
● SensIndoor@ttyACM0.service - SensIndoor Logger
   Loaded: loaded (/etc/systemd/system/SensIndoor@.service; enabled)
   Active: failed (Result: signal) since Wed 2016-11-30 10:36:51 UTC; 37s ago
  Process: 1930 ExecStart=/usr/local/bin/SensIndoor_Logger %I (code=killed, signal=SEGV)
  Process: 1925 ExecStartPre=/bin/chown -R root:root /var/run/SensIndoor (code=exited, status=0/SUCCESS)
  Process: 1922 ExecStartPre=/bin/mkdir /var/run/SensIndoor (code=exited, status=0/SUCCESS)

Nov 30 10:36:45 raspberrypi SensIndoor_Logger[1930]: Starting program with parameter ttyACM0
Nov 30 10:36:45 raspberrypi SensIndoorDaemon[1930]: Daemon starting up
Nov 30 10:36:45 raspberrypi SensIndoorDaemon[1930]: Daemon running
Nov 30 10:36:45 raspberrypi SensIndoor_Logger[1930]: SensIndoorDaemon: Daemon starting up
Nov 30 10:36:45 raspberrypi SensIndoor_Logger[1930]: SensIndoorDaemon: Daemon running
Nov 30 10:36:51 raspberrypi systemd[1]: SensIndoor@ttyACM0.service: control process exited, code=killed status=11
Nov 30 10:36:51 raspberrypi systemd[1]: Failed to start SensIndoor Logger.
Nov 30 10:36:51 raspberrypi systemd[1]: Unit SensIndoor@ttyACM0.service entered failed state.
What's wrong ????

Neo
Last edited by Neo007 on Mon Dec 05, 2016 9:18 am, edited 1 time in total.

tpylkko
Posts: 409
Joined: Tue Oct 14, 2014 5:21 pm

Re: [Systemctl] Own .service file doesn't work

Wed Nov 30, 2016 11:07 am

I suspect that you need to escape all those special symbols in all the commands.
On the use of systemd-escape see:
https://www.freedesktop.org/software/sy ... scape.html

Also:
https://www.freedesktop.org/software/sy ... nd%20lines

Neo007
Posts: 12
Joined: Mon Nov 28, 2016 3:23 pm

Re: [Systemctl] Own .service file doesn't work

Wed Nov 30, 2016 12:31 pm

Thanks tpylkko for your quick reply !

I tried te simplifiy my system file:

Code: Select all

[Unit]
Description=SensIndoor\x20Logger
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/SensIndoor_Logger %I
PIDFile=var-run-SensIndoor.pid

[Install]
WantedBy=default.target
But still don't work:

Code: Select all

root@raspberrypi:/opt/LINUX_SensIndoor_Logger# systemctl status SensIndoor@ttyACM0.service
● SensIndoor@ttyACM0.service - SensIndoor\x20Logger
   Loaded: loaded (/etc/systemd/system/SensIndoor@.service; enabled)
   Active: failed (Result: timeout) since Wed 2016-11-30 12:26:27 UTC; 42s ago
  Process: 2251 ExecStart=/usr/local/bin/SensIndoor_Logger %I (code=killed, signal=TERM)

Nov 30 12:24:57 raspberrypi SensIndoor_Logger[2251]: Starting program with parameter ttyACM0
Nov 30 12:24:57 raspberrypi SensIndoorDaemon[2251]: Daemon starting up
Nov 30 12:24:57 raspberrypi SensIndoorDaemon[2251]: Daemon running
Nov 30 12:24:57 raspberrypi SensIndoor_Logger[2251]: SensIndoorDaemon: Daemon starting up
Nov 30 12:24:57 raspberrypi SensIndoor_Logger[2251]: SensIndoorDaemon: Daemon running
Nov 30 12:26:27 raspberrypi systemd[1]: SensIndoor@ttyACM0.service start operation timed out. Terminating.
Nov 30 12:26:27 raspberrypi systemd[1]: Failed to start SensIndoor\x20Logger.
Nov 30 12:26:27 raspberrypi systemd[1]: Unit SensIndoor@ttyACM0.service entered failed state.
When I launch the program manualy a PID file is created but it isn't when I launch it by the systemd file. Any clue ?

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: [Systemctl] Own .service file doesn't work

Wed Nov 30, 2016 4:47 pm

Neo007 wrote:Hi everyone some one can help me with my systemd file ?

I've made my own program in C who run like a charm. But I want to launch it systemctl.
How would you launch the program from the command line (and have you tested that works)

When you start it does it keep running in the background (i.e. it forks) or does it stay in the foreground ?

Neo007
Posts: 12
Joined: Mon Nov 28, 2016 3:23 pm

Re: [Systemctl] Own .service file doesn't work

Wed Nov 30, 2016 5:54 pm

How would you launch the program from the command line (and have you tested that works)
Simply with this cmd:

Code: Select all

SensIndoor_Logger ttyACM0
It's a USB logger that's why the ACM0 argument
When you start it does it keep running in the background (i.e. it forks) or does it stay in the foreground ?
It's a daemon so in background.

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: [Systemctl] Own .service file doesn't work

Wed Nov 30, 2016 6:11 pm

Neo007 wrote:
How would you launch the program from the command line (and have you tested that works)
Simply with this cmd:

Code: Select all

SensIndoor_Logger ttyACM0
It's a USB logger that's why the ACM0 argument
I'm wondering if there's something in your normal environment that's missing when run with systemd, particularly based on these lines in your output

Code: Select all

  Process: 2251 ExecStart=/usr/local/bin/SensIndoor_Logger %I (code=killed, signal=TERM)
  Process: 1930 ExecStart=/usr/local/bin/SensIndoor_Logger %I (code=killed, signal=SEGV)
You may want to see if there's any extra information from journalctl (and any other logs)
Neo007 wrote:
When you start it does it keep running in the background (i.e. it forks) or does it stay in the foreground ?
It's a daemon so in background.
With systemd (and upstart which some distributions used previously) the fork isn't required and often it's preferred not to fork (although I don't think that's the issue in your case)

Neo007
Posts: 12
Joined: Mon Nov 28, 2016 3:23 pm

Re: [Systemctl] Own .service file doesn't work

Thu Dec 01, 2016 8:38 am

You may want to see if there's any extra information from journalctl (and any other logs)
What other log do you want ?
With systemd (and upstart which some distributions used previously) the fork isn't required and often it's preferred not to fork (although I don't think that's the issue in your case)
So in my code I don't fork and in the systemd file I specify simple not forking ? For information I'm with the lastest raspbian on raspberry pi 1. I will try with the latest raspberry.

If that can help I can post my C code ?

tpylkko
Posts: 409
Joined: Tue Oct 14, 2014 5:21 pm

Re: [Systemctl] Own .service file doesn't work

Thu Dec 01, 2016 9:49 am

Yeah well the second error message in status is diffrent from the fitst one, so something changed there. And the second one is saying the "timed out" which suggests that it is not forking properly. What happens if you just don't use forking type?

Neo007
Posts: 12
Joined: Mon Nov 28, 2016 3:23 pm

Re: [Systemctl] Own .service file doesn't work

Thu Dec 01, 2016 2:32 pm

I just set Type=simple and I have this message

Code: Select all

root@raspberrypi:/opt/LINUX_SensIndoor_Logger# systemctl status SensIndoor@ttyACM0.service
● SensIndoor@ttyACM0.service - SensIndoor Logger
   Loaded: loaded (/etc/systemd/system/SensIndoor@.service; disabled)
   Active: failed (Result: timeout) since Thu 2016-12-01 14:27:47 UTC; 4min 1s ago
  Process: 4006 ExecStart=/usr/local/bin/SensIndoor_Logger %I (code=killed, signal=TERM)

Dec 01 14:26:09 raspberrypi SensIndoor_Logger[4006]: Starting program with parameter ttyACM0
Dec 01 14:26:09 raspberrypi SensIndoorDaemon[4006]: Daemon starting up
Dec 01 14:26:09 raspberrypi SensIndoorDaemon[4006]: Daemon running
Dec 01 14:26:09 raspberrypi SensIndoor_Logger[4006]: SensIndoorDaemon: Daemon starting up
Dec 01 14:26:09 raspberrypi SensIndoor_Logger[4006]: SensIndoorDaemon: Daemon running
Dec 01 14:27:47 raspberrypi systemd[1]: SensIndoor@ttyACM0.service start operation timed out. Terminating.
Dec 01 14:27:47 raspberrypi systemd[1]: Failed to start SensIndoor Logger.
Dec 01 14:27:47 raspberrypi systemd[1]: Unit SensIndoor@ttyACM0.service entered failed state.

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: [Systemctl] Own .service file doesn't work

Thu Dec 01, 2016 4:02 pm

Neo007 wrote:I just set Type=simple and I have this message
You need to also set your code to not fork if you set the type to simple.

In terms of journalctl run that after trying to start the service and you might find additional logged messages that don't show up from the status command.

Neo007
Posts: 12
Joined: Mon Nov 28, 2016 3:23 pm

Re: [Systemctl] Own .service file doesn't work

Fri Dec 02, 2016 9:32 am

And solved !

Code: Select all

● SensIndoor@ttyACM0.service - SensIndoor Logger
   Loaded: loaded (/etc/systemd/system/SensIndoor@.service; disabled)
   Active: active (running) since Fri 2016-12-02 09:23:29 UTC; 6min ago
 Main PID: 3580 (SensIndoor_Logg)
   CGroup: /system.slice/system-SensIndoor.slice/SensIndoor@ttyACM0.service
           └─3580 /usr/local/bin/SensIndoor_Logger ttyACM0

Dec 02 09:23:29 raspberrypi systemd[1]: Started SensIndoor Logger.
Dec 02 09:23:29 raspberrypi SensIndoor_Logger[3580]: Starting program with parameter ttyACM0
Dec 02 09:23:29 raspberrypi SensIndoorDaemon[3580]: Daemon starting up
Dec 02 09:23:29 raspberrypi SensIndoor_Logger[3580]: SensIndoorDaemon: Daemon starting up
Thank you very much ! The weird thing is that I have a PID too....

Now I will try to set an ExecStop rule

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: [Systemctl] Own .service file doesn't work

Fri Dec 02, 2016 10:13 am

Neo007 wrote:And solved !
It might be good to provide some details of what was wrong and how you determined the fault for the next person that has a similar issue (and also for the people who tried to help and are interested / nosy)

Neo007
Posts: 12
Joined: Mon Nov 28, 2016 3:23 pm

Re: [Systemctl] Own .service file doesn't work

Fri Dec 02, 2016 3:41 pm

Yes of course !
Like you proposed I set my systemd file type to simple

Code: Select all

Description=SensIndoor Logger
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/SensIndoor_Logger %I
PIDFile=/var/run/SensIndoor.pid
KillSignal=SIGTERM
KillMode=process

[Install]
WantedBy=default.target
And in my code I deleted the fork cmd and that's it !

Thank you very much for the help !

Return to “Troubleshooting”