How do I make my systemd service run via specific user and start on boot?

void.pointer

I just upgraded from Ubuntu server 14 to version 15. I had trouble getting my upstart script working after the upgrade, and read that systemd is the new default. I'm far from a linux expert, so please go easy on me :-)

Here is what my upstart script was before:

description "NZBGet upstart script"

setuid robert
setgid robert

start on runlevel [2345]
stop on runlevel [016]

respawn

expect fork

script
    exec nzbget -D
end script

pre-stop script
    exec nzbget -Q
end script

Based on the upstart to systemd wiki page, I used the tables provided there to map things as closely as I could in my new systemd service file:

[Unit]
Description=NZBGet Service

[Service]
Type=forking
ExecStart=/usr/local/bin/nzbget -D
ExecStop=/usr/local/bin/nzbget -Q
Restart=on-failure

This file is located at /home/robert/.config/systemd/user/nzbget.service. To start the service manually, I've been doing:

$ systemctl --user start nzbget

This works great. However, when I log out of my SSH session, the service shuts down. Also, it does not start on bootup or user login. I want it to behave the same as it did as an upstart service: I want it to start at boot, run constantly, and as a specific user.

What do I need to do to get this configuration?

Yamaho

First problem

You can specify the directives User= and Group= in the [Service] section of the unit file.

Second problem

To make the service run on boot, you should not put it in your home folder. Instead, put it under /etc/systemd/system/. This is the folder meant to be used by the system administrator (i.e. you) to add new system-wide services.

Other folders include:

  • /usr/lib/systemd/system/ is meant for packages which want to install unit files, though under Debian and Ubuntu the folder is actually /lib/systemd/system/ because the various bin and lib folders have not been merged into a unified /usr/ prefix yet.
  • /usr/local/systemd/system/ is for installing units by locally compiled packages.

Testing the unit

Once the unit file is in an appropriate location, you can try starting the unit immediately by typing systemctl start <UNIT_FILENAME> as usual. It should work without having to type the unit's full path. The extension also doesn't have to be specified if it's .service.

Enabling the unit

Before you can enable your unit, you need to add an [Install] section, under which you should add the directive WantedBy=multi-user.target. This directive specifies the stage of the boot-up process during which the service should be started (if it were enabled). multi-user.target is appropriate for most services.

Once that information is added, you can use systemctl enable <UNIT_FILENAME>, which enables the unit, making systemd from now on automatically start it during boot up at the specified stage.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to make ubuntu service run as specific user

분류에서Dev

How do I setup a systemd service to be started by a non root user as a user daemon?

분류에서Dev

How do I start the mariadb service on NixOS?

분류에서Dev

How do I make space on /boot?

분류에서Dev

Confused why systemd service doesn't start at boot

분류에서Dev

Run 'sudo service mongod start' at boot time

분류에서Dev

start a service a at bootime in systemd

분류에서Dev

how do i make my code shorter

분류에서Dev

How can I make Ubuntu 15.10 run/boot normally?

분류에서Dev

How do I start a tab as a different user from a predefined task?

분류에서Dev

How do I make Ubuntu start fbterm in the tty on startup?

분류에서Dev

How do I make to start docker automatically after reboots?

분류에서Dev

How do I get my Activity Indicator to start at the right time?

분류에서Dev

start systemd-nspawn/systemd container on boot

분류에서Dev

How do I disable a specific CPU core at boot?

분류에서Dev

How do I get Debian to use systemd-logind for user session control?

분류에서Dev

How to start an application at boot time or as a service

분류에서Dev

How do i fix my windows boot loader?

분류에서Dev

How do I make a function run again on a key press?

분류에서Dev

systemd: user service starts at boot with target default/graphical but not multi-user

분류에서Dev

How do I add a new user with specific configuration files?

분류에서Dev

How do I make cron work with a specific app engine module?

분류에서Dev

How do I make Geany my default editor on Ubuntu?

분류에서Dev

How do I make my background fixed in Chrome?

분류에서Dev

How do i make my fragment using a viewPager

분류에서Dev

How to create a service to start my mariadb?

분류에서Dev

Systemd is unmounting my /boot partition

분류에서Dev

How to run the btsync service as the local user?

분류에서Dev

How can I dual boot Ubuntu and Windows 10 but have my PC boot into windows without prompting me at start up

Related 관련 기사

  1. 1

    How to make ubuntu service run as specific user

  2. 2

    How do I setup a systemd service to be started by a non root user as a user daemon?

  3. 3

    How do I start the mariadb service on NixOS?

  4. 4

    How do I make space on /boot?

  5. 5

    Confused why systemd service doesn't start at boot

  6. 6

    Run 'sudo service mongod start' at boot time

  7. 7

    start a service a at bootime in systemd

  8. 8

    how do i make my code shorter

  9. 9

    How can I make Ubuntu 15.10 run/boot normally?

  10. 10

    How do I start a tab as a different user from a predefined task?

  11. 11

    How do I make Ubuntu start fbterm in the tty on startup?

  12. 12

    How do I make to start docker automatically after reboots?

  13. 13

    How do I get my Activity Indicator to start at the right time?

  14. 14

    start systemd-nspawn/systemd container on boot

  15. 15

    How do I disable a specific CPU core at boot?

  16. 16

    How do I get Debian to use systemd-logind for user session control?

  17. 17

    How to start an application at boot time or as a service

  18. 18

    How do i fix my windows boot loader?

  19. 19

    How do I make a function run again on a key press?

  20. 20

    systemd: user service starts at boot with target default/graphical but not multi-user

  21. 21

    How do I add a new user with specific configuration files?

  22. 22

    How do I make cron work with a specific app engine module?

  23. 23

    How do I make Geany my default editor on Ubuntu?

  24. 24

    How do I make my background fixed in Chrome?

  25. 25

    How do i make my fragment using a viewPager

  26. 26

    How to create a service to start my mariadb?

  27. 27

    Systemd is unmounting my /boot partition

  28. 28

    How to run the btsync service as the local user?

  29. 29

    How can I dual boot Ubuntu and Windows 10 but have my PC boot into windows without prompting me at start up

뜨겁다태그

보관