[CentOS] systemctl (again)

Sat Apr 4 19:23:44 UTC 2015
James Hogarth <james.hogarth at gmail.com>

On 4 April 2015 at 18:40, Jonathan Billings <billings at negate.org> wrote:
>
>
> On April 4, 2015 12:14:08 PM EDT, Pete Travis <lists at petetravis.com> wrote:
>>On Apr 4, 2015 7:55 AM, "J Martin Rushton"
>><martinrushton56 at btinternet.com>
>>wrote:
>>>
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>>
>>> Thanks Andrew.
>>>
>>> One more problem solved, as I discovered last thing yesterday there
>>> was a missing "[Install]".  Using your copy of the httpd service I
>>> cut-and-pasted it onto the end of the service file you'd given me
>>> earlier and at last was able to load the service.  It wouldn't run,
>>> but at least it was some progress.
>>>
>>> I ran systemctl daemon-reload and rebooted.
>>>
>>> It is still failing though:
>>>
>>>         # systemctl status timidity
>>>         timidity.service - timidity daemon
>>>            Loaded: loaded (/etc/systemd/system/timidity.service;
>>enabled)
>>>            Active: failed (Result: exit-code) since Sat ...
>>>           Process: 955 ExecStop=/bin/kill -s TERM $MAINPID
>>(code=exited,
>>> status=1/FAILURE)
>>>           Process: 790 ExecStart=/usr/bin/timidity -iAD (code=exited,
>>> status=0/SUCCESS)
>>>          Main PID: 790 (code=exited, status=0/SUCCESS)
>>>
>>
>><snip>
>>
>>The process exited, so systemd thinks the service has exited.  You have
>>a
>>'-D' option, which probably means daemonize, but you haven't set an
>>appropriate Type declaration in the service file.
>>
>>If the service offers it, the best way to do simple services with
>>systemd
>>is with *foreground* options in ExecStart.  Then set Type=simple.
>>STDOUT/STDERR all goes to the journal, making it easier to see what
>>happens
>>if the service legitimately fails.
>>
>>Take a look at packaged files in /usr/lib/systemd/system - plenty of
>>examples to work from.
>>
>>--Pete
>>_______________________________________________
>>CentOS mailing list
>>CentOS at centos.org
>>http://lists.centos.org/mailman/listinfo/centos
>
> Is $MAINPID defined in your pidfile?
>
> It sounds to me like only the 'kill' is exiting with a non-zero exit code because the variable is undefined.
>

This would be better served as the following to accomplish the immediate goal:

cat > /etc/systemd/system/timidity.service <<EOF
 [Unit]
 Description=timidity daemon

 [Service]
 User=jmr
 Group=users
 WorkingDirectory=/home/jmr
 Type=forking
 ExecStart=/usr/bin/timidity -iAD
EOF

systemctl daemon-reload

systemctl start timidity

You don't need to define an ExecStop and the type of the service
should be forking as you are daemonising it - simple if you didn't
daemonise it (or skip type which has this as default).

There is also no need to deal with the race condition mess that is a
PID file with systemd as well... technically there is no need to mess
with MAINPID in this scenario.

This is a quite messy though as you're running a 'system service' in
the context of a regular user.... you were better off doing this
within the user space as you started with.

The reason that failed for you before is that you were making calls to
systemctl without the --user option so it was trying to act in the
system context.

However a google of "timidity systemd" has the arch wiki within the
first few results that has good information which results in a
technically much nicer solution.

https://wiki.archlinux.org/index.php/Timidity#Daemon

Note the --global option which makes it start on a per user basis when
the session for that user starts. Also note they don't daemonise it as
there is no real reason to with systemd controlling the process state.

If you want to stop/start/restart within the context of the user
session you should be doing systemctl --user start/stop/restart
timidity