2

I have set up an init-script to control the state of a 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

The system is CentOS 6.5. If I manually run service [start|stop|status] vtigervm, it works as I expect it to. If the VM is running, $? after the service vtigervm status returns 0, and 3 if it is stopped. I installed it with chkconfig and it created among a few others rc5.d/S99vtigervm and rc0.d/K05vtigervm.

The problem

When I start the system it starts the "service", but on shutdown it does not even run the scripts.

grep "virtual machine" /var/log/messages shows:

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

What I expect:

*[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]*

File permissions & etc:

# 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
3
  • I found this dup which didn't have a workable A. I've flagged the other as a dup to this one, since it has actual solutions to the problem. unix.stackexchange.com/questions/71941/…
    – slm
    Feb 9, 2014 at 1:41
  • @slm I only found one on stackoverflow, which didn't really have an aswer (for me atleast)
    – varesa
    Feb 9, 2014 at 1:47
  • Yeah surprisingly there wasn't a lot of ppl having this particular issue which I find suspicious.
    – slm
    Feb 9, 2014 at 1:49

2 Answers 2

4

Idea #1

Try putting a -x at the top of the service script, this will put the shell into debug mode so that you'll get any output that's being generated by the script.

#!/bin/sh -x

Idea #2

Also you might want to add the process name to the top of the chkconfig comment macros as well.

# processname: vtigervm

You may need to change this value to whatever's appropriate for you situation.

Idea #3

As suggested in @RickBeam's answer and confirmed by this link I found on the CentOS forums, titled: "chkconfig/init.d not calling shutdown with solution", you'll need to manage the creation and destruction of a file in /var/lock/subsys. You can add these lines to your start() and stop() functions to do it:

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

stop() {
...
rm -f /var/lock/subsys/vtigervm
}
9
  • Even if it never starts a process called like that?
    – varesa
    Feb 9, 2014 at 0:47
  • Sorry change the name to whatever it actually is.
    – slm
    Feb 9, 2014 at 0:52
  • Also, where does -x send the output to, if the script is run by init? dmesg//var/log/messages?
    – varesa
    Feb 9, 2014 at 0:59
  • Yes it should go to /var/log/messages.
    – slm
    Feb 9, 2014 at 1:02
  • Your answer isn't an answer. When(if) deleted, the DV will go with it.
    – Ricky
    Feb 9, 2014 at 1:24
2

Your script has to touch /var/lock/subsys/... to indicate it's running. Look at the atd or crond init scripts as an example, and /etc/rc for how it's actually parsed.

(Note: I'm looking at /etc/rc for Fedora)

# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do

    # Check if the subsystem is already up.
    subsys=${i#/etc/rc$runlevel.d/K??}
    [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
            || continue
    check_runlevel "$i" || continue

    # Bring the subsystem down.
    [ -n "$UPSTART" ] && initctl emit --quiet stopping JOB=$subsys
    $i stop
    [ -n "$UPSTART" ] && initctl emit --quiet stopped JOB=$subsys
done
11
  • Ok, I thought status returning valid codes was enough.
    – varesa
    Feb 9, 2014 at 0:47
  • FC2, but CentOS does exactly the same thing. (exact same file, in fact)
    – Ricky
    Feb 9, 2014 at 0:55
  • @slm Maybe it's because of some hard-to-update software? I know a few companies that still have a bunch of old DOS pcs and some Netware stuff in-house
    – varesa
    Feb 9, 2014 at 1:03
  • 1
    It's obviously not an original FC2 (nor is the RH9 machine). Or, the, ahem, RH 4.1 machine. (holy cow, it's running a 14 year old kernel!!!, dual PII-333) There's a RH6.2(?) around here somewhere -- i.e. the last sparc release.
    – Ricky
    Feb 9, 2014 at 1:06
  • @varesa, it's more a factor of "you don't upgrade FC, you reinstall" which is why I've used debian for many years now.
    – Ricky
    Feb 9, 2014 at 1:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .