Shutdown

關機時不會呼叫初始化腳本

  • February 9, 2014

我設置了一個初始化腳本來控制 VirtualBox VM 的狀態:

#!/bin/sh
#chkconfig: 35 99 5
#description: vTiger virtual machine

### BEGIN INIT INFO
# Provides: vtigervm
# Required-Start: $local_fs
# Requider-Stop: $stop_fs
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Manage vTiger virtual machine
# Description: Utility to start and stop vTiger virtual machine on VirtualBox
### END INIT INFO

start()
{
   echo -n "Starting vTiger"
   echo
   su myuser -c '/usr/bin/VBoxManage startvm "vTiger" --type headless'
   echo "Started virtual machine" >> /var/log/messages
}

stop()
{
   echo -n "Shutting vTiger down..."
   echo
   su myuser-c '/usr/bin/VBoxManage controlvm "vTiger" acpipowerbutton'
   while [ ! -z "`su - juhani -c '/usr/bin/VBoxManage list runningvms | grep vTiger'`" ]; do
       echo -n "."
       sleep 1
   done
   echo "Done."
   echo "Stopped virtual machine" >> /var/log/messages
}

status()
{
   echo -n "Running VMs: "
   su myuser -c '/usr/bin/VBoxManage list runningvms'
   echo
   if [ -z "`su - juhani -c '/usr/bin/VBoxManage list runningvms | grep vTiger'`" ]; then
       RETVAL=3    
   else
       RETVAL=0
   fi
   echo "Queried virtual machine status" >> /var/log/messages
}

echo "Called virtual machine management script with: $1" >> /var/log/messages
case "$1" in
 start)
       start
  ;;

 stop)
       stop
  ;;

 restart|try-restart|condrestart|reload)
       stop
       start
  ;;

 status)
       status
  ;;

 *)
       echo $"Usage: $0 {start|stop|restart|status}"
       exit 1
  ;;
esac

exit $RETVAL

系統為 CentOS 6.5。如果我手動執行service [start|stop|status] vtigervm,它會按我的預期工作。如果 VM 正在執行,$?service vtigervm status返回 0,如果停止則返回 3。我安裝了它,chkconfig它在其他幾個rc5.d/S99vtigervmrc0.d/K05vtigervm.

問題

當我啟動系統時,它啟動“服務”,但在關機時它甚至不執行腳本。

grep "virtual machine" /var/log/messages顯示:

*[machine starting]*
Called virtual machine management script with: start
Started virtual machine
*[shutdown -h now]*
*[machine stopped]*

我的期望:

*[machine starting]*
Called virtual machine management script with: start
Started virtual machine 
*[shutdown -h now]*
Called virtual machine management script with: status
Queried virtual machine status
Called virtual machine management script with: stop
Stopped virtual machine
*[machine stopped]*

文件權限等:

# ls -lah /etc/rc0.d/
lrwxrwxrwx. 1 root root 13 7.2. 17:49 /etc/rc0.d/K05atd -> ../initd.d/atd
lrwxrwxrwx. 1 root root 18 9.2. 00:06 /etc/rc0.d/K05vtigervm -> ../initd.d/vtigervm

想法#1

嘗試將 a-x放在服務腳本的頂部,這將使 shell 進入調試模式,以便您獲得腳本生成的任何輸出。

#!/bin/sh -x

想法#2

此外,您可能還希望將程序名稱添加到 chkconfig 註釋宏的頂部。

# processname: vtigervm

您可能需要將此值更改為適合您情況的任何值。

想法#3

正如@RickBeam 的回答所建議的,並通過我在 CentOS 論壇上找到的這個連結確認,標題為:“ chkconfig/init.d not call shutdown with solution ”,您需要管理/var/lock/subsys. 您可以將這些行添加到您的start()stop()函式中來執行此操作:

start()
{
...
touch /var/lock/subsys/vtigervm
}

stop() {
...
rm -f /var/lock/subsys/vtigervm
}

引用自:https://unix.stackexchange.com/questions/114274