简体   繁体   English

如何从Shell脚本中触发延迟的系统关闭?

[英]How can I trigger a delayed system shutdown from in a shell script?

On my private network I have a backup server, which runs a bacula backup every night. 在我的专用网络上,我有一台备份服务器,该服务器每晚运行一次bacula备份。 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. 为了节省能源,我使用cron作业来唤醒服务器,但我还没有发现,如何在备份完成后正确关闭它。 By the means of the bacula-director configuration I can call a script during the processing of the last backup job (ie the backup of the file catalog). 通过bacula-director配置,我可以在处理最后一个备份作业(即文件目录的备份)时调用脚本。 I tried to use this script: 我试着使用这个脚本:

#!/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. 实际上我没有找到明显的方法来在'at'命令的语法中添加所需的shutdown参数。 Maybe someone can give me some advice here. 也许有人可以在这里给我一些建议。

Depending on your backup server's OS, the implementation of shutdown might behave differently. 根据备份服务器的操作系统, shutdown的实现可能会有不同的行为。 I have tested the following two solutions on Ubuntu 12.04 and they both worked for me: 我在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 $? ). shell中脚本的退出代码是正确的(使用echo $?进行测试)。 The shutdown was still in progress (tested with shutdown -c ). 关机仍在进行中(使用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

No need to create a second BASH script to run the shutdown command. 无需创建第二个BASH脚本即可运行shutdown命令。 Just replace the following line in your backup script: 只需在备份脚本中替换以下行:

bash -c "nohup /sbin/shutdown -h 10" &

with this: 有了这个:

echo "/sbin/poweroff" | /usr/bin/at now + 10 min >/dev/null 2>&1

Feel free to adjust the time interval to suit your preference. 您可以随意调整时间间隔以满足您的喜好。

If you can become root: either log in as, or sudo -i this works (tested on ubuntu 14.04): 如果你可以成为root:要么以as登录,要么sudo -i这个工作(在ubuntu 14.04上测试):

# shutdown -h 20:00 & //halts the machine at 8pm # shutdown -h 20:00 & //在晚上8点停止机器

No shell script needed. 不需要shell脚本。 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! 有趣的是,如果我在命令行中尝试使用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点重新启动机器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM