TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Writing systemd units that stop gracefully before shutdown

228 点作者 dghubble超过 2 年前

8 条评论

CGamesPlay超过 2 年前
A much more challenging task is writing a systemd unit that <i>starts</i> gracefully before shutdown. I wanted to write a unit that could issue an API call to delete the instance rather than doing a normal power off. Putting it at a reasonable place in the sequence took a lot of trial and error! The trick is actually to have the unit be <i>started</i> at some point in normal boot up (e.g. “armed”) and then do the actual task when the unit is <i>stopped</i>.<p>Here’s the unit I ended up with: <a href="https:&#x2F;&#x2F;github.com&#x2F;CGamesPlay&#x2F;infra&#x2F;blob&#x2F;master&#x2F;private-server&#x2F;ansible&#x2F;self-destruct.service" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;CGamesPlay&#x2F;infra&#x2F;blob&#x2F;master&#x2F;private-serv...</a>
评论 #33348225 未加载
评论 #33349211 未加载
评论 #33347812 未加载
评论 #33350186 未加载
评论 #33348563 未加载
SoftTalker超过 2 年前
I fundamentally disagree with the idea that software should require or even expect a graceful shutdown. You can never stop the user from yanking the power cord out of the socket, which is what they will do if you force a bunch of housekeeping to happen before shutdown.<p>You have to deal with crash&#x2F;power failure recovery anyway. So do your housekeeping on startup. Shutdown should be a quick and simple termination.
评论 #33348782 未加载
评论 #33348831 未加载
评论 #33348756 未加载
评论 #33348935 未加载
评论 #33354378 未加载
评论 #33349414 未加载
评论 #33350672 未加载
akeck超过 2 年前
I love this. Lots of details I didn&#x27;t know.
MarkusWandel超过 2 年前
Quoting from the article:<p><pre><code> TimeoutStopSec=0 </code></pre> That&#x27;s cost me more than one hard power button powerdon (on a desktop machine with the system partition on SSD - unnerving).<p>One of the innumerable things that systemd stops on shutdown gets stuck - permanently - and the machine goes into a state out of which, to my knowledge, is only a powerdown or reset.<p>I ended up searching for the above and replacing them with a reasonable timeout (several minutes).
评论 #33359729 未加载
rfmoz超过 2 年前
The macOS init manager, LaunchD, doesn&#x27;t offer an easy way to execute an script at shutdown.<p>By definition, it sends a SIGTERM signal to all of the daemons that it started. But as the script isn&#x27;t started before, and doesn&#x27;t keep a running PID, you don&#x27;t have a clean way to do it.<p>I don&#x27;t understand why they only implemented the SIGTERM call without any alternative.
SrslyJosh超过 2 年前
It shouldn&#x27;t be this hard to stop a service gracefully. This is far, far more complicated than SysV init, where you just need to drop a script into &#x2F;etc&#x2F;init.d symlink it from the appropriate rc directories. (For shutdown&#x2F;reboot, you&#x27;d create symlinks in rc5.d and rc6.d named KNNwhatever, where NN is an integer that specifies the order the script will be run in. The &quot;K&quot; stands for &quot;kill&quot;.)<p>Edit: Note that my example runlevels are for Solaris, other UNIX&#x2F;Linux OSes will vary.
评论 #33350562 未加载
kzrdude超过 2 年前
Halt is apparently not the same as poweroff.
评论 #33349583 未加载
评论 #33347355 未加载
exikyut超过 2 年前
Meta: I can&#x27;t reach this website!<p>Chrome is giving me an instant NXDOMAIN error.<p>Dig shows that<p><pre><code> $ dig psdn.io @1.1.1.1 ... ;; -&gt;&gt;HEADER&lt;&lt;- opcode: QUERY, status: NOERROR, id: 19283 ... ;; QUESTION SECTION: ;psdn.io. IN A </code></pre> so then I prefix &quot;www.&quot; like is in the URL...<p><pre><code> $ dig www.psdn.io @1.1.1.1 ... ;; -&gt;&gt;HEADER&lt;&lt;- opcode: QUERY, status: NXDOMAIN, id: 64024 ... ;; QUESTION SECTION: ;www.psdn.io. IN A ;; ANSWER SECTION: www.psdn.io. 300 IN CNAME poseidon-www.pages.dev. </code></pre> Okay, fine:<p><pre><code> $ dig poseidon-www.pages.dev @1.1.1.1 ... ;; -&gt;&gt;HEADER&lt;&lt;- opcode: QUERY, status: NXDOMAIN, id: 55471 ... ;; QUESTION SECTION: ;poseidon-www.pages.dev. IN A </code></pre> ...wat??<p>(Where there&#x27;s no ANSWER section, none was returned, just an AUTHORITY section)<p>This is reproducible for me with 1.1.1.1, 8.8.8.8 and 9.9.9.9.
评论 #33350016 未加载