sid*_*com 79 linux shutdown systemd
我需要在该[install]
部分中添加什么,以便 systemd/home/me/so.pl
在关机之前和/proc/self/net/dev
被销毁之前运行?
[Unit]
Description=Log Traffic
[Service]
ExecStart=/home/me/so.pl
[Install]
?
Run Code Online (Sandbox Code Playgroud)
小智 90
建议的解决方案是将服务单元作为正常服务运行 - 查看[Install]
部分。所以一切都必须逆向思考,依赖也是如此。因为关机顺序是反向启动顺序。这就是为什么脚本必须放在ExecStop=
.
以下解决方案对我有用:
[Unit]
Description=...
[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=<your script/program>
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
RemainAfterExit=true
当你没有ExecStart
动作时需要它。
创建文件后,请确保systemctl daemon-reload
和systemctl enable yourservice --now
。
我刚从 systemd IRC 得到它,学分将归于 mezcalero。
Pio*_*icz 11
要在启动任何重启/关闭/停止/kexec 服务之前(即在根文件系统重新挂载为只读之前的最后一刻)运行服务,请使用以下服务配置:
[Unit]
Description=Save system clock on shutdown
DefaultDependencies=no
After=final.target
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/fake-hwclock.sh save
[Install]
WantedBy=final.target
Run Code Online (Sandbox Code Playgroud)
启用它:
systemctl enable my_service.service
Run Code Online (Sandbox Code Playgroud)要在实际重新启动/关闭/停止/kexec之前运行脚本(当您无法写入根文件系统时,因为它以只读方式重新安装)将此脚本可执行文件添加到/usr/lib/systemd/system-shutdown
目录中。
在执行实际系统halt/poweroff/reboot/kexec systemd-shutdown 之前,将运行/usr/lib/systemd/system-shutdown/ 中的所有可执行文件,并向它们传递一个参数:“halt”、“poweroff”、“reboot” " 或 "kexec",取决于选择的操作。此目录中的所有可执行文件都是并行执行的,并且在所有可执行文件完成之前不会继续执行操作。
另见:
https://www.freedesktop.org/software/systemd/man/bootup.html
https://www.freedesktop.org/software/systemd/man/systemd-halt.service.html
据我所知,这满足了我的需要(但我不知道确切原因)。
[Unit]
Description=Log Traffic
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
[Service]
ExecStart=/usr/local/bin/perl /home/me/log_traffic.pl --stop
Type=oneshot
Run Code Online (Sandbox Code Playgroud)
我不完全确定,但我认为您不需要安装部分,尽管我明确添加了它。我也没有测试它,但我认为它应该可以帮助你开始:
[Unit]
Description=Log Traffic
Requires=network.target
After=network.target
Before=shutdown.target
DefaultDependencies=no
[Service]
ExecStart=/home/me/so.pl
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=shutdown.target
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
118507 次 |
最近记录: |