bash 如何在 shell 脚本中触发延迟系统关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20046908/
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 can I trigger a delayed system shutdown from in a shell script?
提问by Peter S.
On my private network I have a backup server, which runs a bacula backup every night. To save energy I use a cron job to wake the server, but I haven't found out, how to properly shut it down after the backup is done. By the means of the bacula-director configuration I can call a script during the processing of the last backup job (i.e. the backup of the file catalog). I tried to use this script:
在我的专用网络上,我有一个备份服务器,它每晚运行 bacula 备份。为了节省能源,我使用 cron 作业来唤醒服务器,但我还没有发现如何在备份完成后正确关闭它。通过bacula-director 配置,我可以在处理上次备份作业(即文件目录的备份)期间调用脚本。我尝试使用这个脚本:
#!/bin/bash
# shutdown server in 10 minutes
#
# ps, 17.11.2013
bash -c "nohup /sbin/shutdown -h 10" &
exit 0
The script shuts down the server - but apparently it returns just during the shutdown, and as a consequence that last backup job hangs just until the shutdown. How can I make the script to file the shutdown and return immediately?
该脚本关闭服务器 - 但显然它在关闭期间返回,因此最后一个备份作业挂起直到关闭。如何让脚本文件关闭并立即返回?
Update: After an extensive search I came up with a (albeit pretty ugly) solution: The script run by bacula looks like this:
更新:经过广泛的搜索,我想出了一个(虽然很丑陋)的解决方案: bacula 运行的脚本如下所示:
#!/bin/bash
at -f /root/scripts/shutdown_now.sh now + 10 minutes
And the second script (shutdown_now.sh) looks like this:
第二个脚本 (shutdown_now.sh) 如下所示:
#!/bin/bash
shutdown -h now
Actually I found no obvious method to add the required parameters of shutdown in the syntax of the 'at' command. Maybe someone can give me some advice here.
实际上我没有发现明显的方法可以在'at'命令的语法中添加shutdown所需的参数。也许有人可以在这里给我一些建议。
回答by Chriki
Depending on your backup server's OS, the implementation of shutdown
might behave differently. I have tested the following two solutions on Ubuntu 12.04 and they both worked for me:
根据备份服务器的操作系统, 的实现shutdown
可能会有所不同。我已经在 Ubuntu 12.04 上测试了以下两个解决方案,它们都对我有用:
As the root user I have created a shell script with the following content and called it in a bash shell:
作为 root 用户,我创建了一个包含以下内容的 shell 脚本,并在 bash shell 中调用它:
shutdown -h 10 &
exit 0
The exit code of the script in the shell was correct (tested with echo $?
). The shutdown was still in progress (tested with shutdown -c
).
shell 中脚本的退出代码是正确的(用 测试echo $?
)。关闭仍在进行中(用 测试shutdown -c
)。
This bash function call in a second shell script worked equally well:
第二个 shell 脚本中的这个 bash 函数调用同样有效:
my_shutdown() {
shutdown -h 10
}
my_shutdown &
exit 0
回答by Brian Showalter
No need to create a second BASH script to run the shutdown command. Just replace the following line in your backup script:
无需创建第二个 BASH 脚本来运行关闭命令。只需替换备份脚本中的以下行:
bash -c "nohup /sbin/shutdown -h 10" &
bash -c "nohup /sbin/shutdown -h 10" &
with this:
有了这个:
echo "/sbin/poweroff" | /usr/bin/at now + 10 min >/dev/null 2>&1
echo "/sbin/poweroff" | /usr/bin/at now + 10 min >/dev/null 2>&1
Feel free to adjust the time interval to suit your preference.
随意调整时间间隔以满足您的喜好。
回答by Aaron
If you can become root: either log in as, or sudo -i
this works (tested on ubuntu 14.04):
如果你可以成为 root:要么以身份登录,要么sudo -i
这样工作(在 ubuntu 14.04 上测试):
# shutdown -h 20:00 &
//halts the machine at 8pm
# shutdown -h 20:00 &
//在晚上8点停止机器
No shell script needed. I can then log out, and log back in, and the process is still there. Interestingly, if I tried this with sudo
in the command line, then when I log out, the process does go away!
不需要shell脚本。然后我可以注销,然后重新登录,该过程仍然存在。有趣的是,如果我sudo
在命令行中尝试过这个,那么当我注销时,这个过程会消失!
BTW, just to note, that I also use this command to do occasional reboots after everyone has gone home.
顺便说一句,请注意,我也使用此命令在每个人都回家后偶尔重新启动。
# shutdown -r 20:00 &
//re-boots the machine at 8pm
# shutdown -r 20:00 &
//晚上8点重新启动机器