LoginSignup
2
2

More than 5 years have passed since last update.

VMの起動が終わるのを判定してバッチ処理を行う方法

Posted at

私のパソコンでは起動時にVBoxManage.exe startvm hogehoge --type headlessというカンジでバッチで流して起動している。
このあとにputtyやらAptanaを起動しているのだが、VMが完全に立ち上げる前に起動してしまうので困っていた。
sleep処理で待っても良いのだが、どうもスマートじゃない。
そこでsftpコマンドを利用して下記を書いてみたら、上手く待ってくれるようになった。

sftp -oPort=<ポート番号>  -oConnectionAttempts=120 -b "./bye.bat" <ユーザ名>@<アドレス>
bye.bat
bye

これは、sftpコマンドを120回試して、うまく繋がったらbye.batを流すというコマンドになっている。-oでssh_configの設定が出来ることを利用してみた。
ConnectionAttemptsは接続を試みる回数を指定している。一回一秒なので、120秒=2分である。2分試して上手くいかなければエラーということで。
bye.batは何の処理も必要ないので、すぐ接続を切るようにbyeのみである。

これはWindows環境でやっているが、ロジック的にはMac・Linuxでも問題ないはずだ。
というかWindowsでやるにはコマンドからsftpを打てるようにする必要がある!
参考までに、私が普段起動しているバッチを載せておきます。

startVM.bat
rem VM起動
"<PATH>\VBoxManage.exe" startvm "<VM名>" --type headless
sftp -oPort=<ポート番号>  -oConnectionAttempts=120 -b "./bye.bat" <ユーザ名>@<アドレス>

rem Aptana起動
start <PATH>\AptanaStudio3.exe

rem Chrome起動
start <PATH>\chrome.exe --new-window http://XXXX http://YYYY http://ZZZZ

rem putty起動
<PATH>\PUTTY.EXE -load <設定名> -l <ユーザ名> -pw <パスワード>

参考:
http://webkaru.net/linux/sftp-command/
http://www.yosbits.com/opensonar/rest/man/freebsd/man/ja/man5/ssh_config.5.html

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2