SHOW:
|
|
- or go back to the newest paste.
1 | - | ;;; systemd system service example |
1 | + | ;;; systemd user service example |
2 | - | ; See also https://pastebin.com/uqY2DcGs for a user service example. |
2 | + | ; See also https://pastebin.com/KXVdTNrL for a system service example. |
3 | ; | |
4 | - | ; Save as this file: /etc/systemd/system/my-service.service |
4 | + | ; Make sure the directory exists (you only need to do this once): |
5 | ; mkdir -p ~/.config/systemd/user | |
6 | ; | |
7 | - | ; in the commands below), as long as it doesn't conflict with any existing system |
7 | + | ; Save this file as: ~/.config/systemd/user/my-service.service |
8 | - | ; services (see: systemctl list-unit-files '*.service'). |
8 | + | |
9 | ; You can use any name instead of "my-service" of course (but then also adjust that | |
10 | ; in the commands below), as long as it doesn't conflict with any existing user | |
11 | ; services (see: systemctl --user list-unit-files '*.service'). | |
12 | ; | |
13 | ; The file must have a ".service" suffix but you may omit that suffix in the various | |
14 | ; commands below, i.e. you can write "my-service" instead of "my-service.service". | |
15 | - | ; sudo systemctl daemon-reload |
15 | + | |
16 | ; | |
17 | ; Then, tell systemd to reload its configuration: | |
18 | - | ; sudo systemctl start my-service.service |
18 | + | ; systemctl --user daemon-reload |
19 | ; | |
20 | - | ; If that works, enable your service to start at boot: |
20 | + | |
21 | - | ; sudo systemctl enable my-service.service |
21 | + | ; systemctl --user start my-service.service |
22 | ; | |
23 | ; Check on the status of your service: | |
24 | ; systemctl --user status my-service.service | |
25 | - | ; journalctl --follow --unit=my-service.service |
25 | + | |
26 | - | ; which you can also abbreviate to: |
26 | + | ; If that works, enable your service to start automatically: |
27 | - | ; journalctl -f -u my-service |
27 | + | ; systemctl --user enable my-service.service |
28 | ; | |
29 | ; Make sure the systemd user instance for the "debian" user is started at boot, | |
30 | ; otherwise your service(s) will only run when you're logged in: | |
31 | ; sudo loginctl enable-linger debian | |
32 | ; (you only need to do this once) | |
33 | ; | |
34 | ; | |
35 | ; Follow log output related to your service (use control-C to exit): | |
36 | ; journalctl -f --user-unit=my-service.service | |
37 | ; | |
38 | ; | |
39 | - | ; For a list of special targets that may be useful as dependencies or |
39 | + | |
40 | - | ; for ordering constraints, see: |
40 | + | |
41 | - | ; man systemd.special |
41 | + | |
42 | ; | |
43 | ; For directives available in the [Service] section of service units, see: | |
44 | ; man systemd.service | |
45 | ; man systemd.exec | |
46 | ; man systemd.resource-control | |
47 | ; | |
48 | ; Note that comment lines can either start with ; or with #. Using # is | |
49 | ; more common, but pastebin's syntax highlighting doesn't recognize # but | |
50 | - | Description=My system service |
50 | + | |
51 | ||
52 | - | ; It may be necessary to include dependencies to prevent the script from |
52 | + | |
53 | - | ; running too early during boot. |
53 | + | |
54 | ||
55 | - | ; For example, if you need networking, you probably want: |
55 | + | Description=My user service |
56 | - | Wants=network-online.target |
56 | + | |
57 | - | After=network-online.target |
57 | + | |
58 | [Install] | |
59 | - | ; If you need stuff in /dev, you probably want: |
59 | + | |
60 | - | Wants=systemd-udev-settle.service |
60 | + | ; When enabled, start service at startup |
61 | - | After=systemd-udev-settle.service |
61 | + | WantedBy=default.target |
62 | ||
63 | ||
64 | [Service] | |
65 | ||
66 | - | ; When enabled, start service at boot |
66 | + | |
67 | - | WantedBy=multi-user.target |
67 | + | |
68 | ; or | |
69 | ;Type=exec | |
70 | ; The latter is better for diagnostics since if the program fails to start (e.g. | |
71 | ; the path in ExecStart is wrong) then with Type=exec the service will fail to | |
72 | ; start while with Type=simple the service will initially seem to start but then | |
73 | ; fail immediately afterwards. However, Type=exec is unavailable in older versions | |
74 | ; of systemd (e.g. if you're still using debian stretch) and Type=simple is also | |
75 | ; slightly more efficient than Type=exec | |
76 | ; | |
77 | ; If your program instead just does some setup and then exits, use: | |
78 | ;Type=oneshot | |
79 | ; and in this case you probably also want to set: | |
80 | ;RemainAfterExit=yes | |
81 | ; (see the systemd.service man page for more details on these options) | |
82 | ||
83 | - | ; By default the service runs as root, but you can choose a different user: |
83 | + | ; By default the service runs in ~, but you can choose a different working dir: |
84 | - | User=debian |
84 | + | ;WorkingDirectory=~ |
85 | ||
86 | - | ; By default the service runs in /, but you can choose a different working dir: |
86 | + | |
87 | - | WorkingDirectory=/home/debian |
87 | + |