Starting systemd user services on boot on a Debian server

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Starting systemd user services on boot on a Debian server

Fredrik Jonson-2
Hi,

What is the recommended way to set up Systemd user services with Debian?

With previous Debian releases I've been running services like tomcat from
regular user accounts by creating wrapper scripts that controls the PATH and
environment variables, and start those scripts with @reboot from the user's
crontab.

While this works ok, I find that the scripts are hard to maintan and does not
meet all requirements of service managment. It would be nice to migrate
away from my fragile scripts to something that's dedicated to controlling
services, like Systemd units.

AFAIU, to make this happen, I need to have a user Systemd instance started on
boot. How do I set up my Debian server to automatically start a user instance
for a regular user on boot to control the user's service units?  Is there a
recommended way to do it?

BTW, if I just try to start systemd from the console, it fails:

 $ systemd --user
 Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.

Clearly that is not the way to do it. Any advice?

--
Fredrik Jonson

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server

Ansgar Burchardt-9
Fredrik Jonson <[hidden email]> writes:
> What is the recommended way to set up Systemd user services with Debian?
[...]
> AFAIU, to make this happen, I need to have a user Systemd instance started on
> boot. How do I set up my Debian server to automatically start a user instance
> for a regular user on boot to control the user's service units?  Is there a
> recommended way to do it?

Just run

  loginctl enable-linger [USER...]

as root. This will make logind start the systemd user instance for the
given user(s).

Ansgar

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server

Fredrik Jonson-2
Ansgar Burchardt wrote:
>  Fredrik Jonson <[hidden email]> writes:
> > What is the recommended way to set up Systemd user services with Debian?
>
>  Just run 'loginctl enable-linger [USER]' as root.

Initially that didn't work...

 root@host# loginctl enable-linger foouser
 Failed to create bus connection: No such file or directory

A quick google seems to indicate[0][1] that it is related to dbus somehow.
I checked, and the server didn't have dbus installed on the machine, so I
installed it (apt-get install dbus) and reran the command:

 root@host# loginctl enable-linger foouser

This time the command ran without any apparent objections from the system.

Next, I create my unit in $HOME/.config/systemd/user/foo.service but
systemctl fails to enable it:

 foouser@host$ systemctl --user enable foo.service
 Failed to get D-Bus connection: Connection refused

Now what?

I notice that there isn't a systemd or dbus instance running under the user.
Should there be, and if, what's responsible for starting it?

Additionally, when I try to start a user instance manually, it fails:

 foouser@host$ systemd --user
 Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.

[0] http://immae.eu/blog/2014/05/26/manage-your-session-with-systemd/
[1] https://lists.archlinux.org/pipermail/arch-general/2014-May.txt

--
Fredrik Jonson

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server

Ansgar Burchardt-9
Fredrik Jonson <[hidden email]> writes:

> Ansgar Burchardt wrote:
>>  Fredrik Jonson <[hidden email]> writes:
>> > What is the recommended way to set up Systemd user services with Debian?
>>  Just run 'loginctl enable-linger [USER]' as root.
>
> Initially that didn't work...
>
>  root@host# loginctl enable-linger foouser
>  Failed to create bus connection: No such file or directory
>
> A quick google seems to indicate[0][1] that it is related to dbus somehow.
> I checked, and the server didn't have dbus installed on the machine, so I
> installed it (apt-get install dbus) and reran the command:

Hmm, right. logind needs dbus to work.

>  root@host# loginctl enable-linger foouser
>
> This time the command ran without any apparent objections from the system.
>
> Next, I create my unit in $HOME/.config/systemd/user/foo.service but
> systemctl fails to enable it:
>
>  foouser@host$ systemctl --user enable foo.service
>  Failed to get D-Bus connection: Connection refused
>
> Now what?
>
> I notice that there isn't a systemd or dbus instance running under the user.
> Should there be, and if, what's responsible for starting it?
>
> Additionally, when I try to start a user instance manually, it fails:
>
>  foouser@host$ systemd --user
>  Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.

The documentation for "loginctl enable-linger" says "If enabled for a
specific user, a user manager is spawned for the user at boot and kept
around after logouts." I guess you didn't restart the system?

A user manager should in either case also be started when logging
in. This is handled by logind + libpam-systemd. I assume you might not
have the latter installed? (Note that using "su" to switch from root to
foouser does not count as a login.)

I don't think you can just start "systemd --user" as it wants to manage
cgroups for services it spawns. "/lib/systemd/system/user@.service" has
Delegate=yes to allow it to do so.

libpam-systemd is also responsible for setting the XDG_RUNTIME_DIR
variable.

libpam-systemd is currently at Priority: optional. I'm wondering if it
should be raised to Priority: standard...

Ansgar

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Fredrik Jonson-2
Ansgar Burchardt wrote:

>  Fredrik Jonson <[hidden email]> writes:
> > What is the recommended way to enable Systemd user services with Debian?
> [...]
>  The documentation for "loginctl enable-linger" says "If enabled for a
>  specific user, a user manager is spawned for the user at boot and kept
>  around after logouts." I guess you didn't restart the system?
>
>  [...]
>
>  A user manager should in either case also be started when logging
>  in. This is handled by logind + libpam-systemd. I assume you might not
>  have the latter installed?

Good guess, that's another culprit identified:

 foouser@host$ sudo apt-get install libpam-systemd
 foouser@host$ sudo shutdown -r now
 [... the system reboots]
 [... I've previously created ~/.config/systemd/user/foo.service]
 foouser@host$ systemctl --user enable foo
 foouser@host$ systemctl --user start foo

Succes!

What a great friday afternoon. Debian is awesome!

>  I don't think you can just start "systemd --user" as it wants to manage
>  cgroups for services it spawns. "/lib/systemd/system/user@.service" has
>  Delegate=yes to allow it to do so.

Right, I was just aimlessly typing commands that I hoped where anywhere close
to relevant to resolving the problem. :)

>  libpam-systemd is also responsible for setting the XDG_RUNTIME_DIR
>  variable.
>
>  libpam-systemd is currently at Priority: optional. I'm wondering if it
>  should be raised to Priority: standard.

Maybe. I gleaned at the package documentation and wasn't totally convinced it
was relevant. There was already a file /etc/pam.d/systemd-user from the systemd
package, so I didn't understand that I also had to install another package that
provided the actual pam module. Would it not make more sense to have the pam
config in the same package that provides the actual pam module?

Still there might have been relevant keywords that I should have picked up on,
the package description does state:

 This package contains the PAM module which registers user sessions in
 the systemd control group hierarchy.

OTOH, in the next paragraph I read the following statement, that definitively
steered me away from installing it:

 If in doubt, do install this package.

When debian developers tells me in their package documentation that I really
should not install their package, I tend to follow their advice. :)

Anyway, it works now.

In summary, to enable systemd user units on boot in Debian, the following steps
are required:

 sudo apt-get install dbus libpam-systemd
 sudo loginctl enable-linger [USER]
 sudo shutdown -r now

Ansgar, I'm greatful, thanks for all your advice!

--
Fredrik Jonson

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Michael Biebl-3
Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:

>  If in doubt, do install this package.
>
> When debian developers tells me in their package documentation that I really
> should not install their package, I tend to follow their advice. :)

 Did you maybe misread that as
"If in doubt, do *not* install this package".

Otherwise your replay doesn't make sense, unless you meant it sarcastic
in some kind of way.


--
Why is it that all of the instruments seeking intelligent life in the
universe are pointed away from Earth?


signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Fredrik Jonson-2
Michael Biebl wrote:

>  Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:
>
> >  If in doubt, do install this package.
>
>   Did you maybe misread that as
>  "If in doubt, do *not* install this package".

Yes I did. I don't know how many times I've read that sentence, and clearly saw
a _not_ in there. Interesting.

--
Fredrik Jonson

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Lisi Reisz
On Saturday 17 October 2015 06:24:10 Fredrik Jonson wrote:
> Michael Biebl wrote:
> >  Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:
> > >  If in doubt, do install this package.
> >
> >   Did you maybe misread that as
> >  "If in doubt, do *not* install this package".
>
> Yes I did. I don't know how many times I've read that sentence, and clearly
> saw a _not_ in there. Interesting.

The structure foreshadows a "not".  A more natural feeling way of expressing
the positive would be:

If in doubt, install this package.

Lisi

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Fredrik Jonson-2
In <[hidden email]> Lisi Reisz wrote:

>  On Saturday 17 October 2015 06:24:10 Fredrik Jonson wrote:
> > Michael Biebl wrote:
> > >  Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:
> > > >  If in doubt, do install this package.
> > >
> > >   Did you maybe misread that as
> > >  "If in doubt, do *not* install this package".
> >
> > Yes I did. I don't know how many times I've read that sentence, and clearly
> > saw a _not_ in there. Interesting.
>
>  The structure foreshadows a "not".

Maybe the presence of the entire sentence indicates that the package really
should be installed by default on systems where systemd is installed?

--
Fredrik Jonson

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Michael Biebl-3
Am 17.10.2015 um 10:20 schrieb Fredrik Jonson:

> In <[hidden email]> Lisi Reisz wrote:
>>  On Saturday 17 October 2015 06:24:10 Fredrik Jonson wrote:
>>> Michael Biebl wrote:
>>>>  Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:
>>>>>  If in doubt, do install this package.
>>>>
>>>>   Did you maybe misread that as
>>>>  "If in doubt, do *not* install this package".
>>>
>>> Yes I did. I don't know how many times I've read that sentence, and clearly
>>> saw a _not_ in there. Interesting.
>>
>>  The structure foreshadows a "not".
>
> Maybe the presence of the entire sentence indicates that the package really
> should be installed by default on systems where systemd is installed?
>
I'm not a native speaker, so I'm happy to rephrase the message to make
it more obvious. Suggestions?

We might also consider bumping the prio of libpam-systemd to standard,
as Ansgar suggested. That means, it would be installed by default,
unless you explicitly deselect the standard task in d-i.

--
Why is it that all of the instruments seeking intelligent life in the
universe are pointed away from Earth?


signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Mart van de Wege
Michael Biebl <[hidden email]> writes:

> Am 17.10.2015 um 10:20 schrieb Fredrik Jonson:
>> In <[hidden email]> Lisi Reisz wrote:
>>>  On Saturday 17 October 2015 06:24:10 Fredrik Jonson wrote:
>>>> Michael Biebl wrote:
>>>>>  Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:
>>>>>>  If in doubt, do install this package.
>>>>>
>>>>>   Did you maybe misread that as
>>>>>  "If in doubt, do *not* install this package".
>>>>
>>>> Yes I did. I don't know how many times I've read that sentence, and clearly
>>>> saw a _not_ in there. Interesting.
>>>
>>>  The structure foreshadows a "not".
>>
>> Maybe the presence of the entire sentence indicates that the package really
>> should be installed by default on systems where systemd is installed?
>>
>
> I'm not a native speaker, so I'm happy to rephrase the message to make
> it more obvious. Suggestions?

The use of 'do' in that way suggests emphasis. If you wanted that
emphasis, a more unambiguous way is

  If in doubt, *install* this package

If the emphasis is unnecessary, just leave out the the 'do'.

Mart

--
"We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Brian
On Sat 17 Oct 2015 at 15:01:55 +0200, Mart van de Wege wrote:

> Michael Biebl <[hidden email]> writes:
>
> > Am 17.10.2015 um 10:20 schrieb Fredrik Jonson:
> >> In <[hidden email]> Lisi Reisz wrote:
> >>>  On Saturday 17 October 2015 06:24:10 Fredrik Jonson wrote:
> >>>> Michael Biebl wrote:
> >>>>>  Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:
> >>>>>>  If in doubt, do install this package.
> >>>>>
> >>>>>   Did you maybe misread that as
> >>>>>  "If in doubt, do *not* install this package".
> >>>>
> >>>> Yes I did. I don't know how many times I've read that sentence, and clearly
> >>>> saw a _not_ in there. Interesting.
> >>>
> >>>  The structure foreshadows a "not".
> >>
> >> Maybe the presence of the entire sentence indicates that the package really
> >> should be installed by default on systems where systemd is installed?
> >>
> >
> > I'm not a native speaker, so I'm happy to rephrase the message to make
> > it more obvious. Suggestions?
>
> The use of 'do' in that way suggests emphasis. If you wanted that
> emphasis, a more unambiguous way is
>
>   If in doubt, *install* this package
>
> If the emphasis is unnecessary, just leave out the the 'do'.

The sentence is unambiguous (there no word "not" in it); it has a hint
of instruction in it but is perfectly good English as it stands. Leaving
out "do" is still good English but is more requesting or invitational.

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Curt
On 2015-10-17, Brian <[hidden email]> wrote:

>>
>> The use of 'do' in that way suggests emphasis. If you wanted that
>> emphasis, a more unambiguous way is
>>
>>   If in doubt, *install* this package
>>
>> If the emphasis is unnecessary, just leave out the the 'do'.
>
> The sentence is unambiguous (there no word "not" in it); it has a hint
> of instruction in it but is perfectly good English as it stands. Leaving
> out "do" is still good English but is more requesting or invitational.
>
>

I find the 'do' rather chummy myself.

"If you're in the neighborhood, do stop by."


Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Brian
In reply to this post by Michael Biebl-3
On Sat 17 Oct 2015 at 13:29:48 +0200, Michael Biebl wrote:

> We might also consider bumping the prio of libpam-systemd to standard,
> as Ansgar suggested. That means, it would be installed by default,
> unless you explicitly deselect the standard task in d-i.

Looking at the policy manual at

  https://www.debian.org/doc/debian-policy/ch-archive.html

we see
   
    standard

    These packages provide a reasonably small but not too limited
    *character-mode* system. This is what will be installed by
    default if the user doesn't select anything else. It doesn't
    include many large applications.

(The emphasis is mine).

Unless I am misunderstanding something, libpam-systemd has a focus on
non-character-mode systems.

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Lisi Reisz
In reply to this post by Michael Biebl-3
On Saturday 17 October 2015 12:29:48 Michael Biebl wrote:

> Am 17.10.2015 um 10:20 schrieb Fredrik Jonson:
> > In <[hidden email]> Lisi Reisz wrote:
> >>  On Saturday 17 October 2015 06:24:10 Fredrik Jonson wrote:
> >>> Michael Biebl wrote:
> >>>>  Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:
> >>>>>  If in doubt, do install this package.
> >>>>
> >>>>   Did you maybe misread that as
> >>>>  "If in doubt, do *not* install this package".
> >>>
> >>> Yes I did. I don't know how many times I've read that sentence, and
> >>> clearly saw a _not_ in there. Interesting.
> >>
> >>  The structure foreshadows a "not".
> >
> > Maybe the presence of the entire sentence indicates that the package
> > really should be installed by default on systems where systemd is
> > installed?
>
> I'm not a native speaker, so I'm happy to rephrase the message to make
> it more obvious. Suggestions?

"If in doubt, install this package."

That "do" makes one expect the "not" that isn't there, and even assume that it
is meant to be there, if one has noticed that it isn't.  

Lisi

>
> We might also consider bumping the prio of libpam-systemd to standard,
> as Ansgar suggested. That means, it would be installed by default,
> unless you explicitly deselect the standard task in d-i.

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Brian
On Sat 17 Oct 2015 at 19:40:50 +0100, Lisi Reisz wrote:

> On Saturday 17 October 2015 12:29:48 Michael Biebl wrote:
> > Am 17.10.2015 um 10:20 schrieb Fredrik Jonson:
> > > In <[hidden email]> Lisi Reisz wrote:
> > >>  On Saturday 17 October 2015 06:24:10 Fredrik Jonson wrote:
> > >>> Michael Biebl wrote:
> > >>>>  Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:
> > >>>>>  If in doubt, do install this package.
> > >>>>
> > >>>>   Did you maybe misread that as
> > >>>>  "If in doubt, do *not* install this package".
> > >>>
> > >>> Yes I did. I don't know how many times I've read that sentence, and
> > >>> clearly saw a _not_ in there. Interesting.
> > >>
> > >>  The structure foreshadows a "not".
> > >
> > > Maybe the presence of the entire sentence indicates that the package
> > > really should be installed by default on systems where systemd is
> > > installed?
> >
> > I'm not a native speaker, so I'm happy to rephrase the message to make
> > it more obvious. Suggestions?
>
> "If in doubt, install this package."
>
> That "do" makes one expect the "not" that isn't there, and even assume that it
> is meant to be there, if one has noticed that it isn't.  

As my spouse says:

  "When you go to the Co-op, please do get butter."

Her expectations are not necessarily met by the eventual outcome. :)

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Lisi Reisz
On Saturday 17 October 2015 20:19:48 Brian wrote:

> On Sat 17 Oct 2015 at 19:40:50 +0100, Lisi Reisz wrote:
> > On Saturday 17 October 2015 12:29:48 Michael Biebl wrote:
> > > Am 17.10.2015 um 10:20 schrieb Fredrik Jonson:
> > > > In <[hidden email]> Lisi Reisz wrote:
> > > >>  On Saturday 17 October 2015 06:24:10 Fredrik Jonson wrote:
> > > >>> Michael Biebl wrote:
> > > >>>>  Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:
> > > >>>>>  If in doubt, do install this package.
> > > >>>>
> > > >>>>   Did you maybe misread that as
> > > >>>>  "If in doubt, do *not* install this package".
> > > >>>
> > > >>> Yes I did. I don't know how many times I've read that sentence, and
> > > >>> clearly saw a _not_ in there. Interesting.
> > > >>
> > > >>  The structure foreshadows a "not".
> > > >
> > > > Maybe the presence of the entire sentence indicates that the package
> > > > really should be installed by default on systems where systemd is
> > > > installed?
> > >
> > > I'm not a native speaker, so I'm happy to rephrase the message to make
> > > it more obvious. Suggestions?
> >
> > "If in doubt, install this package."
> >
> > That "do" makes one expect the "not" that isn't there, and even assume
> > that it is meant to be there, if one has noticed that it isn't.
>
> As my spouse says:
>
>   "When you go to the Co-op, please do get butter."
>
> Her expectations are not necessarily met by the eventual outcome. :)

:-)

Yes, but she says it, not writes it.  You would get a slight (?  depending on
whether you forgot last time ;-)  ) stress on the "do". ;-)

Lisi

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Brian
On Sat 17 Oct 2015 at 22:52:36 +0100, Lisi Reisz wrote:

> On Saturday 17 October 2015 20:19:48 Brian wrote:
> >
> > As my spouse says:
> >
> >   "When you go to the Co-op, please do get butter."
> >
> > Her expectations are not necessarily met by the eventual outcome. :)
>
> :-)
>
> Yes, but she says it, not writes it.  You would get a slight (?  depending on
> whether you forgot last time ;-)  ) stress on the "do". ;-)

Indeed. The stress is almost always there in conversational English.

If I were to rewrite part of the package description I would have

  If in doubt install this package.

No comma. Its not needed.

  Install this package if in doubt.

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

Joel Rees-3
On Sun, Oct 18, 2015 at 7:34 AM, Brian <[hidden email]> wrote:

> On Sat 17 Oct 2015 at 22:52:36 +0100, Lisi Reisz wrote:
>
>> On Saturday 17 October 2015 20:19:48 Brian wrote:
>> >
>> > As my spouse says:
>> >
>> >   "When you go to the Co-op, please do get butter."
>> >
>> > Her expectations are not necessarily met by the eventual outcome. :)
>>
>> :-)
>>
>> Yes, but she says it, not writes it.  You would get a slight (?  depending on
>> whether you forgot last time ;-)  ) stress on the "do". ;-)
>
> Indeed. The stress is almost always there in conversational English.
>
> If I were to rewrite part of the package description I would have
>
>   If in doubt install this package.
>
> No comma. Its not needed.
>
>   Install this package if in doubt.
>

How about,

    When in doubt, definitely install this package.

or, to guide the reader more carefully,

    This package is installed by default. Unless you have a very good
reason and know what you are doing, leave it that way.

--
Joel Rees

Be careful when you look at conspiracy.
Arm yourself with knowledge of yourself, as well:
http://reiisi.blogspot.jp/2011/10/conspiracy-theories.html

Reply | Threaded
Open this post in threaded view
|

Re: Starting systemd user services on boot on a Debian server [SOLVED]

cbannister
In reply to this post by Fredrik Jonson-2
On Sat, Oct 17, 2015 at 05:24:10AM +0000, Fredrik Jonson wrote:

> Michael Biebl wrote:
>
> >  Am 16.10.2015 um 19:05 schrieb Fredrik Jonson:
> >
> > >  If in doubt, do install this package.
> >
> >   Did you maybe misread that as
> >  "If in doubt, do *not* install this package".
>
> Yes I did. I don't know how many times I've read that sentence, and clearly saw
> a _not_ in there. Interesting.

Weird, I think I (mis)read it that way as well. Better would be
'If in doubt, install this package.'

--
"If you're not careful, the newspapers will have you hating the people
who are being oppressed, and loving the people who are doing the
oppressing." --- Malcolm X

12