rc script problems
Eugene Grosbein
eugen at grosbein.net
Sun Mar 31 21:18:55 UTC 2019
01.04.2019 3:55, Jim Trigg wrote:
> How do I write an rc script to preserve the pid of a child process? The "port" I'm working with (technically a commercial package) has a startup script that launches a java process. I had written a simple rc wrapper around the script but have found it doesn't support stop because the pid captured is the script's pid rather than the java process's pid.
Use something like this for startup script "portname.in" for Java-based long running service:
#!/bin/sh
#
# $FreeBSD$
#
# PROVIDE: portname
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf to enable `portname':
#
# portname_enable="YES"
. /etc/rc.subr
command_interpreter="."
command="%%PREFIX%%/bin/java"
command_args="-jar portname.jar"
name="%%PORTNAME%%"
rcvar="${name}_enable"
start_cmd="start_cmd"
stop_cmd="stop_cmd"
pidfile="/var/run/${name}.pid"
required_files="%%DATADIR%%/portname.jar"
portname_chdir="%%DATADIR%%"
eval `JAVAVM_DRYRUN=yes ${command} | fgrep JAVAVM_COMMAND`
procname=${JAVAVM_COMMAND}
start_cmd()
{
check_startmsgs && echo "Starting ${name}."
cd ${portname_chdir}
daemon -u %%USERS%% -f -p ${pidfile} ${command} ${command_args} start
}
stop_cmd()
{
check_startmsgs && echo "Stopping ${name}."
rc_pid=$(check_pidfile $pidfile $procname)
pkill -F ${pidfile}
wait_for_pids $rc_pid
}
load_rc_config ${name}
: ${portname_enable="NO"}
run_rc_command "$1"
#EOF
That is, use daemon(8) utility that can act as superviser for java process:
start it with needed arguments, write its pid to pidfile etc.
More information about the freebsd-ports
mailing list