关于rc.local中自定义命令不能开机执行的问题

最近阳光影视和FTP所在的虚拟机主机出了点问题,原先设置的开机自动启动两个虚拟机实例不知道为何失效了,每次停电或者其他情况服务器重启后,需要手动启动虚拟机才行。有时没有及时发现,就会影响服务的使用。今天趁着早起,使用VOD和FTP的用户还少,想看一下是什么原因。开机启动虚拟机是在rc.local里面设定的,以下为服务器上的rc.local文件内容。

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#/etc/init.d/vboxweb-service start
mount /dev/sdb1 /data
VBoxManage startvm win2003 -type headless
VBoxManage startvm FTP_Debian -type headless
exit 0

从上面可以看出,这实际上是一个bash脚本,既然是脚本,应该手动运行它也没问题,我就试着运行了下,结果出现了一行错误提示:
mount: special device /dev/sdb1 does not exist 字面意思就是运行mount这个命令时出错啦,/dev/sdb1这个设备不存在。我想起来了,/dev/sdb1这个磁盘是原来测试时挂载的iscsi磁盘,不存在?我输入fdisk -l,回车后发现果然不存在了,原来是open-iscsi服务没有运行,我打开iscsi服务,/dev/sdb1出现了,这时再次运行rc.local这个文件,这次提示变了,内容大意是指定的虚拟机已经运行了。到这里,我明白了,之所以虚拟机不能启动是因为它之前的一条命令出错了,命令就终止了,这时我又注意到,rc.local这个文件里面的一句提示:
# Make sure that the script will "exit 0" on success or any other # value on error. 大家都知道,我们在初学C语言时,main函数最后一句是 return 0;这其实是程序的返回值,0代表正常运行。这里的提示说要确保运行的脚本能在成功时返回0,失败的时候返回其他的值。但是出错时也有返回值啊,为什么就不能继续了呢,我还不得而知。为了保证命令能成功运行,即使出错也能继续,我在每行命令后面添了一个&,让它后台运行,这样就保证返回值为0了。

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
#/etc/init.d/vboxweb-service start
mount /dev/sdb1 /data&
VBoxManage startvm win2003 -type headless&
VBoxManage startvm FTP_Debian -type headless&
exit 0
1,897 views, since 2012-05-15