Writing an rc(8) script for a java process

I've written a rc(8) script for a minecraft server. The server is just a java process so I used daemon(8) to detach it from the terminal. The script works great for starting the server, but doesn't seem to be able to stop it. It appears the check_pidfile function in rc.subr(8) wants the process in the pid file to match the command as it was started, so its looking for /usr/sbin/daemon, but in reality the process is java, as illustrated below:

Code:
# ps -auwwwxp `cat /var/run/minecraft.pid`
USER        PID %CPU %MEM   VSZ   RSS  TT  STAT STARTED      TIME COMMAND
minecraft 84330  0.0 24.4 1260232 252340  ??  Is    1:50PM   0:28.34 /usr/local/jdk1.6.0/bin/java -Xmx1024M -Xms512M -jar /usr/local/minecraft/minecraft_server.jar nogui

Running the script with sh -x shows the relevent bits from check_pidfile

Code:
+ eval 'rc_pid=$(check_pidfile' /var/run/minecraft.pid /usr/sbin/daemon ')'
+ check_pidfile /var/run/minecraft.pid /usr/sbin/daemon
+ _pidfile=/var/run/minecraft.pid
+ _procname=/usr/sbin/daemon
+ _interpreter=''
+ [ -z /var/run/minecraft.pid -o -z /usr/sbin/daemon ]
+ [ ! -f /var/run/minecraft.pid ]
+ read _pid _junk
+ [ -z 84330 ]
+ _find_processes /usr/sbin/daemon . '-p 84330'
+ [ 3 -ne 3 ]
+ _procname=/usr/sbin/daemon
+ _interpreter=.
+ _psargs='-p 84330'
+ _pref=''


I can think of a bunch of ugly ways to solve this, but I'm wondering if anyone has any good solution for this?

The script is below:

Code:
#!/bin/sh
# PROVIDE: minecraft
# REQUIRE: NETWORKING SERVERS
# BEFORE: DAEMON
# KEYWORD: shutdown


minecraft_enable="${minecraft_enable:-"NO"}"
minecraft_home="${minecraft_home:-"/home/minecraft"}"
minecraft_server="${minecraft_server:-"minecraft_server.jar"}"
minecraft_flags="${minecraft_flags:-"nogui"}"
minecraft_java_version="${minecraft_java_version:-"1.6+"}"
minecraft_java_opts="${minecraft_java_opts:-"-Xmx1024M -Xms512M"}"
minecraft_user="${minecraft_user:-"minecraft"}"

. /etc/rc.subr

name="minecraft"
rcvar=`set_rcvar`
pidfile="/var/run/minecraft.pid"

load_rc_config "${name}"

if [ -n "${minecraft_java_home}" ] ; then
        export JAVA_HOME="${minecraft_java_home}"
fi

if [ -n "${minecraft_java_version}" ] ; then
        export JAVA_VERSION="${minecraft_java_version}"
fi

if [ -n "${minecraft_java_vendor}" ] ; then
        export JAVA_VENDOR="${minecraft_java_vendor}"
fi

if [ -n "${minecraft_java_os}" ] ; then
        export JAVA_OS="${minecraft_java_os}"
fi

# Required we have minecraft server jar
required_files="${minecraft_home}/${minecraft_server}"

# Build the command line
java_command="/usr/local/bin/java $minecraft_java_opts -jar $minecraft_home/$minecraft_server"

# We need the minecraft jar to do anything
required_files="${minecraft_home}/${minecraft_server}"

command="/usr/sbin/daemon"
flags="-f -p ${pidfile} ${java_command} ${minecraft_flags} ${log_args}"

start_precmd=pid_touch

pid_touch ()
{
        touch $pidfile
        chown $minecraft_user $pidfile
}

cd "$minecraft_home"
run_rc_command "$1"

Also, the cd to $minecraft_home has always felt kind of ugly. I couldn't find anything about it, but does the rc system have a way to change directories before starting the command? The minecraft server needs to have its working directory set to its home before starting.
 
Back
Top