OpenVZ Forum


Home » General » Support » Execute Script on HN after VM Start (I want to execute a script on the hostnode after the VM has started)
Execute Script on HN after VM Start [message #38992] Sat, 27 February 2010 19:00 Go to next message
bigbohne is currently offline  bigbohne
Messages: 3
Registered: February 2010
Junior Member
Hi there,

I've got a little problem that's driving me crazy.
Does someone of you know how to start a script in the hostnodes context AFTER the VM is up an running.

"<VEID>.mounts" - is executed before the vm starts in HN context
"<VEID>.start" - is executed after the vm stared. But in VM context.

My specs:
Debian Lenny
Kernel: 2.6.26-2-openvz-686
vzctl version 3.0.22

Thanks for any help!
Bigbohne
Re: Execute Script on HN after VM Start [message #38998 is a reply to message #38992] Mon, 01 March 2010 23:20 Go to previous messageGo to next message
mcarreira is currently offline  mcarreira
Messages: 30
Registered: February 2007
Member
I think there is not a custom way of doing that. You must be inventive Smile
Re: Execute Script on HN after VM Start [message #39015 is a reply to message #38992] Thu, 04 March 2010 08:45 Go to previous messageGo to next message
TheStig is currently offline  TheStig
Messages: 94
Registered: December 2008
Member
You could include a script at the end of the mount-script that sleeps for a couple of seconds, checks if the VPS has started up correctly (using vzlist) and does whatever you want it to do on the HN.
Re: Execute Script on HN after VM Start [message #39017 is a reply to message #38992] Thu, 04 March 2010 08:54 Go to previous messageGo to next message
maratrus is currently offline  maratrus
Messages: 1495
Registered: August 2007
Location: Moscow
Senior Member
It can be performed in the same way as on a common Linux machine.
Just put whatever you want to do in /etc/rc.local.
Re: Execute Script on HN after VM Start [message #39024 is a reply to message #38992] Thu, 04 March 2010 09:17 Go to previous messageGo to next message
TheStig is currently offline  TheStig
Messages: 94
Registered: December 2008
Member
he want's his script exec after the start of a VPS on the HN not the VPS. I don't see how the init-scrips could help him with that?!
Re: Execute Script on HN after VM Start [message #39026 is a reply to message #39015] Thu, 04 March 2010 09:34 Go to previous messageGo to next message
bigbohne is currently offline  bigbohne
Messages: 3
Registered: February 2010
Junior Member
TheStig wrote on Thu, 04 March 2010 03:45
You could include a script at the end of the mount-script that sleeps for a couple of seconds, checks if the VPS has started up correctly (using vzlist) and does whatever you want it to do on the HN.


I think this is the best method.
My first aproche was to modify the script in the init.d folder which executes a script after the start of a specific VM.

I will post some sample code and try to add in into the wiki for further use Smile

Thanks alot,
Bigbohne
Re: Execute Script on HN after VM Start [message #39027 is a reply to message #38992] Thu, 04 March 2010 12:22 Go to previous messageGo to next message
TheStig is currently offline  TheStig
Messages: 94
Registered: December 2008
Member
let me save you the trouble:

/etc/vz/conf/103.mount
#!/bin/bash
. /etc/vz/start_script/103.sh &
disown 
exit 0


/etc/vz/start_stript/103.sh
#!/bin/bash
_sleep() {
sleep 4
status=(`/usr/sbin/vzctl status 103`)
x=1
until [ $x == 6 ] ; do
sleep 1
	if [ ${status[4]} == "running" ] ; then
		CMD
		MORE CMD
		exit 0
	else
		x=`expr $x + 1`
	fi
done
}
_sleep

The latter would wait once for five seconds and then try it again every second for five times.

Alternatively, if you want to wait forever, use
#!/bin/bash
sleep 5
_sleep() {
status=(`/usr/sbin/vzctl status 103`)
if [ ${status[4]} == "running" ] ; then
	CMD
	MORE CMD
	exit 0
fi
sleep 1
_sleep
}
_sleep

[Updated on: Thu, 04 March 2010 12:23]

Report message to a moderator

Re: Execute Script on HN after VM Start [message #39028 is a reply to message #39027] Thu, 04 March 2010 15:39 Go to previous messageGo to next message
bigbohne is currently offline  bigbohne
Messages: 3
Registered: February 2010
Junior Member
Oh, thank you very much! Smile

How about an wiki entry?
Maybe in the Howto-Section.
I think that more than one people would be interested in this solution.

regards,
bigbohne
Re: Execute Script on HN after VM Start [message #39029 is a reply to message #38992] Thu, 04 March 2010 15:53 Go to previous message
TheStig is currently offline  TheStig
Messages: 94
Registered: December 2008
Member
if you think somebody else could need that piece of bashscript feel free to amend the wiki.

beware: the "disown" command is afaik absolutely necessary. otherwise vzctl won't resume the start command until the secondary script finished exec.
Previous Topic: HN default internal IP
Next Topic: Ubuntu 9.04 wrong permissions on /dev/null after restart
Goto Forum:
  


Current Time: Thu Mar 28 17:26:01 GMT 2024

Total time taken to generate the page: 0.01826 seconds