我正在编写一项systemd
服务,它实际上会做一些“愚蠢”的事情,例如在关机时显示一条屏幕消息,说一些诸如“感谢您使用这台电脑。我们很高兴今天能够为您提供帮助。祝您有美好的一天。”
如果我的关机脚本真的执行(运行),它应该将上述消息放入纯文本文件中,/root/MyScripts/Shutdown/test.txt
但它就是不起作用,尽管在过去六周里我几乎不断地绞尽脑汁尝试了许多不同的方法。
这是我的测试消息脚本,应该在systemd
关机前立即调用,但却失败了。
#ShutdownMesg.sh
#!/bin/bash
echo -e 'Hello, World! \nHave a good day!' >> test.txt
z=0
for i in {1..3}; do
sleep 1m
((z++))
echo ""
done
chmod u+x ShutdownMesg.sh
请注意,我包含了一个简单的循环,以便系统插入 3 分钟的暂停,以确保我的测试文件在实际关机之前有时间被写入。
我的服务代码systemd
:
[Unit]
Description=Create a simple test message at shutdown but will add more useful shutdown commands later
DefaultDependencies=no
Before=poweroff.target
[Service]
Type=forking
RemainAfterExit=yes
ExecStart=/root/MyScripts/Shutdown/ShutdownMesg.sh
[Install]
WantedBy=poweroff.target
创建上述服务后,我做了以下操作:
将其放在一个纯文本文件中,/usr/lib/systemd/system/myshutdownmesg.service
然后使用 启用它,systemctl enable myshutdownmesg.service
然后执行systemctl daemon-reload
,接着执行systemctl list-unit-files
。
当最后一条命令运行时,我得到了一长串服务列表。我的特定服务以及其他服务列在屏幕最左侧的一列中;第二列显示我的服务已启用,但第三列显示它已禁用。
执行时systemctl start myshutdownmesg.service
,我收到以下错误消息:
Job for myshutdownmesg.service failed because the control process exited with error code.
See "systemctl status myshutdownmesg.service" and "journalctl -xeu myshutdownmesg.service" for details.
我的脚本有什么问题吗?我是不是漏掉了什么?由于这是一个要在关机时执行的脚本,我需要向内核传递一些特殊参数或者重新编译它吗?顺便说一句,我已经禁用了 SeLinux,但稍后运行脚本后我会启用它。