简体   繁体   English

如何从Shell脚本返回然后重新启动(与VirtualBox一起使用)?

[英]How can I return from a shell script and then reboot (to use with VirtualBox)?

I am using guestcontrol with Virtual Box with a Windows host and a Linux (RHEL7) guest. 我在带有Windows主机和Linux(RHEL7)来宾的Virtual Box中使用guestcontrol。 I want to do some config from the host to the guest by running a shell script on the guest (from a .bat on the host). 我想通过在来宾上运行Shell脚本(从主机上的.bat)来从主机到来宾进行一些配置。 This is fine and the script runs, however, it hangs when I call the reboot (I believe it is because nothing is returned). 这很好并且脚本可以运行,但是,当我调用重新引导时,脚本会挂起(我相信是因为什么都没有返回)。 So when the following .sh is called: 因此,当调用以下.sh时:

#!/bin/bash
echo "here"
exit

The .bat file shows "here" and then exits (or if I use pause gives the correct message). .bat文件显示“此处”,然后退出(或者如果我使用pause,则会给出正确的消息)。 However, when I add the reboot, the .bat never processes anything past where it calls the script. 但是,当我添加重启时,.bat永远不会处理任何超出其调用脚本位置的内容。 I think this would be because the guest never tells the host that the script is complete. 我认为这是因为来宾从未告诉主机脚本已完成。

I have tried things like: 我已经尝试过类似的事情:

#!/bin/bash
{ sleep 1; reboot; } >/dev/null &
exit

or even: 甚至:

#!/bin/bash
do_reboot(){
  sleep 1
  reboot
}
do_reboot() &
exit

but the .bat never gets past the line where it runs the .sh 但是.bat永远不会超过运行.sh的行

How can I tell the host that the .sh script (on the guest) is complete so it can continue with the .bat script? 我怎样才能告诉主机.sh脚本(在客户机上)已完成,以便可以继续使用.bat脚本?

We need to make sure there are no sub processes running, so we want to do a no heads up using the nohup command. 我们需要确保没有正在运行的子进程,因此我们要使用nohup命令进行提示。 So the script simply becomes this: 因此脚本就是这样的:

#!/bin/bash

nohup reboot &> /tmp/nohup.out </dev/null &
exit

The stdin and stdout were causing the issues, so this just sends them into the void so that the script will not be waiting for any input from any other processes. stdin和stdout引起了问题,因此这只是将它们发送到void中,因此脚本将不会等待任何其他进程的任何输入。

If you have any issues with this script, you could do something like: 如果此脚本有任何问题,可以执行以下操作:

#!/bin/bash

nohup /path/to/reboot_delay.sh &> /tmp/nohup.out </dev/null &
exit

And then in /path/to/reboot_delay.sh you would have: 然后在/path/to/reboot_delay.sh中,您将拥有:

#!/bin/bash
sleep 10 # or however many seconds you need to wait for something to happen
reboot

This way you could even allow some time for something to finish etc, yet the host machine (or ssh or wherever you are calling this from) would still know the script had finished and do what it needs to do. 这样,您甚至可以花一些时间来完成某件事,等等,但是主机(或ssh或您从何处调用它)仍然会知道脚本已经完成并需要做的事情。

I hope this can help people in future. 我希望这可以在将来对人们有所帮助。

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

相关问题 如何在不创建子Shell的情况下从Shell脚本中的函数调用返回值? - How can I return a value from a function call in a shell script without creating a subshell? 如何创建通用的shell命令以从执行或源脚本中退出或返回? - How can I create a generic shell command to exit or return from a script that is either executed or sourced? 如何将sqlplus中受影响的行数返回到Shell脚本? - How can I return the number of rows affected in sqlplus to a shell script? 如何在新的终端窗口或标签中使用从终端外壳脚本设置的变量? (OSX) - How can I use variables set from a terminal shell script in a new terminal window or tab? (OSX) 如何在 shell 脚本中声明和使用布尔变量? - How can I declare and use Boolean variables in a shell script? 如何在shell脚本中使用“ time”命令? - How can I use the `time` command IN a shell script? 我可以在bash shell脚本中使用正则表达式吗 - can i use regex in a bash shell script 如何捕获从Ruby调用的shell脚本的输出? - How can I capture the output of a shell script invoked from Ruby? 如何从Shell脚本中触发延迟的系统关闭? - How can I trigger a delayed system shutdown from in a shell script? 如何从bash脚本运行django shell命令 - How can I run django shell commands from a bash script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM