简体   繁体   中英

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. 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). 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:

#!/bin/bash
echo "here"
exit

The .bat file shows "here" and then exits (or if I use pause gives the correct message). However, when I add the reboot, the .bat never processes anything past where it calls the script. 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

How can I tell the host that the .sh script (on the guest) is complete so it can continue with the .bat script?

We need to make sure there are no sub processes running, so we want to do a no heads up using the nohup command. 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.

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:

#!/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.

I hope this can help people in future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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