「Systemd」:修訂間差異

出自Gea-Suan Lin's Wiki
跳至導覽 跳至搜尋
本頁面具有訪問限制。如果您看見此訊息,這代表您沒有訪問本頁面的權限。
 
(未顯示同一使用者於中間所作的 16 次修訂)
行 1: 行 1:
{{Lowercase}}
{{Lowercase}}
'''systemd'''是一個系統套件。
'''systemd'''是一個系統套件。
== 設定 ==
system模式下不需要額外設定就可以用,建議將自製的service檔案裝到<code>/etc/systemd/system</code>下;user模式下需要針對特定使用者啟動user自己的systemd:
<syntaxhighlight lang="bash">
sudo loginctl enable-linger foo
</syntaxhighlight>
另外在<code>~/.profile</code>內可能會需要指定<code>XDG_RUNTIME_DIR</code>:
<syntaxhighlight lang="bash">
[ -z ${XDG_RUNTIME_DIR} ] && export XDG_RUNTIME_DIR=/run/user/$(id -u)
</syntaxhighlight>


== 範例 ==
== 範例 ==


 這是一個 不完整 的範例:
 這是一個 很簡單 的範例 ,放在<code>/etc/systemd/system/</code>下


<syntaxhighlight lang="ini">
<syntaxhighlight lang="ini">
行 13: 行 27:


[Service]
[Service]
#Environment="HTTP_PROXY=http://127.0.0.1:3128/"
#Environment="HTTPS_PROXY=http://127.0.0.1:3128/"
ExecStart=/usr/bin/spawn-fcgi
ExecStart=/usr/bin/spawn-fcgi
Restart=always
#ExecStop=...
#KillSignal=SIGQUIT # Default is SIGTERM
Restart=on-failure
RestartSec=60
RestartSec=60
Type=simple
Type=simple
#WorkingDirectory=


[Install]
[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
</syntaxhighlight>
</syntaxhighlight>
這是user模式下的(因為user模式下沒有<code>multi-user.target</code>,也不需要<code>remote-fs.target</code>),放在<code>~/.config/systemd/user/</code>下:
<syntaxhighlight lang="ini">
#
[Unit]
Description=Description
[Service]
#Environment="HTTP_PROXY=http://127.0.0.1:3128/"
#Environment="HTTPS_PROXY=http://127.0.0.1:3128/"
ExecStart=/usr/bin/spawn-fcgi
#ExecStop=...
#KillSignal=SIGQUIT # Default is SIGTERM
Restart=on-failure
RestartSec=60
Type=simple
#WorkingDirectory=
[Install]
WantedBy=default.target
</syntaxhighlight>
對於<code>Type</code>的選擇<ref>{{Cite web |url=https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type= |title=systemd.service |language=en |accessdate=2023-10-16}}</ref>,常用的有<code>simple</code>(繼續執行)與<code>forking</code>(類似daemon mode)。
== 參考文獻 ==
{{Reflist|2}}


== 外部連結 ==
== 外部連結 ==

於 2024年1月23日 (二) 10:07 的最新修訂

systemd是一個系統套件。

設定

system模式下不需要額外設定就可以用,建議將自製的service檔案裝到/etc/systemd/system下;user模式下需要針對特定使用者啟動user自己的systemd:

sudo loginctl enable-linger foo

另外在~/.profile內可能會需要指定XDG_RUNTIME_DIR

[ -z ${XDG_RUNTIME_DIR} ] && export XDG_RUNTIME_DIR=/run/user/$(id -u)

範例

這是一個很簡單的範例,放在/etc/systemd/system/下:

#
[Unit]
Description=Description
After=remote-fs.target

[Service]
#Environment="HTTP_PROXY=http://127.0.0.1:3128/"
#Environment="HTTPS_PROXY=http://127.0.0.1:3128/"
ExecStart=/usr/bin/spawn-fcgi
#ExecStop=...
#KillSignal=SIGQUIT # Default is SIGTERM
Restart=on-failure
RestartSec=60
Type=simple
#WorkingDirectory=

[Install]
WantedBy=multi-user.target

這是user模式下的(因為user模式下沒有multi-user.target,也不需要remote-fs.target),放在~/.config/systemd/user/下:

#
[Unit]
Description=Description

[Service]
#Environment="HTTP_PROXY=http://127.0.0.1:3128/"
#Environment="HTTPS_PROXY=http://127.0.0.1:3128/"
ExecStart=/usr/bin/spawn-fcgi
#ExecStop=...
#KillSignal=SIGQUIT # Default is SIGTERM
Restart=on-failure
RestartSec=60
Type=simple
#WorkingDirectory=

[Install]
WantedBy=default.target

對於Type的選擇[1],常用的有simple(繼續執行)與forking(類似daemon mode)。

參考文獻

  1. systemd.service. [2023-10-16] (English). 

外部連結