Linux 如何在 CentOS 关机前运行 Shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18891312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a Shell Script before shutdown on CentOS
提问by linuxnewbee
I want to send an email when the system is going to shutdown to an email ID. I have CentOS 6.4. Below is my Script.
我想在系统关闭时发送电子邮件到电子邮件 ID。我有 CentOS 6.4。下面是我的脚本。
cat /ect/init.d/sendshtmail
#!/bin/bash
EMAIL="[email protected]"
SHUTDOWNSUBJECT="["`hostname`"] - System Shutdown"
SHUTDOWNBODY="This is an automated message to notify you that "`hostname`" is shutting down.
LOCKFILE=/var/lock/subsys/SystemEmail
echo "${SHUTDOWNBODY}" | mutt -s "${SHUTDOWNSUBJECT}" ${EMAIL}
It has the appropriate permission. While running it manually it's working perfectly. I have just symlinked it to /etc/rc0.d/ folder. By issuing below command.
它具有适当的权限。手动运行它时,它运行良好。我刚刚将它符号链接到 /etc/rc0.d/ 文件夹。通过发出以下命令。
ln -s /etc/init.d/sendshtmail /etc/rc0.d/K00sendshtmail
But the script is not sending any email during shutdown. Thanks in Advance.
但是脚本在关机期间不发送任何电子邮件。提前致谢。
采纳答案by konsolebox
Try to set executable permissions for your script. Sometimes you need to do that to activate it.
尝试为您的脚本设置可执行权限。有时您需要这样做才能激活它。
chmod 755 /etc/init.d/sendshtmail
Also try to use absolute paths for your command, while quoting the other variable as well.
还尝试为您的命令使用绝对路径,同时引用另一个变量。
echo "${SHUTDOWNBODY}" | /usr/bin/mutt -s "${SHUTDOWNSUBJECT}" "${EMAIL}"
Another attempt is to switch your user to your current user e.g.
另一种尝试是将您的用户切换到当前用户,例如
echo "${SHUTDOWNBODY}" | su -l -c "/usr/bin/mutt -s \"${SHUTDOWNSUBJECT}\" \"${EMAIL}\"" yourusername
回答by suhas
ln -s /etc/init.d/sendshtmail /etc/rc0.d/S01sendshtmail
ln -s /etc/init.d/sendshtmail /etc/rc0.d/S01sendshtmail
The symlink name should begin with a S - for Start (K for Kill)
符号链接名称应以 S 开头 - 代表开始(K 代表杀死)
The two-digit specifies the order of execution for your script, the lowest numbered being execute first.
两位数指定脚本的执行顺序,编号最小的先执行。
回答by Mahattam
Place your shell script in /etc/init.d with executable permission and symlink name should start with K##. If you want to execute your script at first place immediately after shut down then name it with K00scriptname. Script started will K will be executed first based on ascending order then script with S.
将您的 shell 脚本放在 /etc/init.d 中,并具有可执行权限,符号链接名称应以 K## 开头。如果您想在关闭后立即首先执行您的脚本,则将其命名为 K00scriptname。启动的脚本将首先根据升序执行 K,然后使用 S 执行脚本。
ln -s /etc/init.d/script /etc/rc0.d/K00scriptname
ln -s /etc/init.d/script /etc/rc0.d/K00scriptname
Shutdown command will send the stop signal to script, your script (K00scriptname) should have stop function like example
关机命令会将停止信号发送到脚本,您的脚本(K00scriptname)应该具有停止功能,例如
stop()
{
echo "executing scriptname"
"Your script logic"
}
case "" in
stop)
stop
;;
esac
Most important, K00scriptname will execute only if there would be lock file present in /var/lock/subsys folder, so do "touch /var/lock/subsys/scriptname" then check by doing shutdown.
最重要的是,K00scriptname 仅在 /var/lock/subsys 文件夹中存在锁定文件时才会执行,因此请执行“touch /var/lock/subsys/scriptname”然后通过执行关闭来检查。