在关机之前可以运行脚本吗?这意味着在系统仍能充分发挥作用的时候。
我试图将我的关闭脚本放在/lib/systemd/system-shutdown/test-script中。它将被执行,但据我所知,它只在关闭发生之前立即执行。例如,磁盘已经以只读模式挂载.这是可以修复的,但是用以下方法包装脚本:
#!/bin/bash
mount -oremount,rw /
... do something ...
mount -oremount,ro /不过,我需要和无法工作的是互联网连接。当脚本被执行时,internet连接似乎不再可用。
我在测试Ubuntu 16.04。
发布于 2017-05-08 11:22:58
我在https://unix.stackexchange.com/q/39226/29509上找到了答案。
总之,这个解决方案适合我(在Ubuntu 16.04上):
创建一个文件/etc/systemd/system/my-cleanup.service:
[Unit]
Description=Test cleanup at shutdown
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/path/to/my-cleanup-script.sh
[Install]
WantedBy=multi-user.target(假设关闭脚本在/path/to/my-cleanup-script.sh下可用,并且是可执行的。)
最后,通过以下方式启用systemd服务:
sudo systemctl enable my-cleanup.service
sudo systemctl start my-cleanup.service发布于 2017-05-08 14:54:19
关于答案1,我相信解决方案将在“关闭期间”起作用,但正如OP要求it will run right at the begin of the shutdown的那样,我不确定。
此链接更明确地说明在关机期间执行脚本的时间:http://ccm.net/faq/3348-execute-a-script-at-startup-and-shutdown-on-ubuntu
请注意,我没有在我的系统中测试它,我只是在传递我的发现。
https://askubuntu.com/questions/913010
复制相似问题