Skip to content

Instantly share code, notes, and snippets.

@Wu-Wu
Created October 3, 2016 18:21
Show Gist options
  • Save Wu-Wu/7ac87fb895215b52126474cc05936e72 to your computer and use it in GitHub Desktop.
Save Wu-Wu/7ac87fb895215b52126474cc05936e72 to your computer and use it in GitHub Desktop.
psgiapp
#!/bin/sh
#
# PROVIDE: psgiapp
# REQUIRE: NETWORKING
# KEYWORD: shutdown
. /etc/rc.subr
case "$0" in
/etc/rc*)
# during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
# so get the name of the script from $_file
name=$(basename "$_file" .sh)
;;
*)
name=$(basename "$0" .sh)
;;
esac
rcvar=$(set_rcvar)
load_rc_config $name
eval ": \${${name}_enable:=\"NO\"}"
eval ": \${${name}_appfile:=\"/usr/local/etc/${name}.psgi\"}"
eval ": \${${name}_owner:=\"www\"}"
eval ": \${${name}_group:=\"www\"}"
eval ": \${${name}_pidfile:=\"/var/run/starman/${name}.pid\"}"
eval ": \${${name}_flags:=\"\"}"
eval ": \${${name}_listen:=\"\"}"
appfile="$(eval echo \${${name}_appfile})"
pidfile="$(eval echo \${${name}_pidfile})"
appowner="$(eval echo \${${name}_owner})"
appgroup="$(eval echo \${${name}_group})"
appflags="$(eval echo \${${name}_flags})"
listen="$(eval echo \${${name}_listen})"
perlbrew_root="$(eval echo \${${name}_perlbrew_root})"
perlbrew_home="$(eval echo \${${name}_perlbrew_home})"
perlbrew_lib="$(eval echo \${${name}_perlbrew_lib})"
statusfile="/var/tmp/${name}.status"
start_precmd="psgiapp_precmd"
start_cmd="psgiapp_start"
stop_cmd="psgiapp_stop"
status_cmd="psgiapp_status"
pb_lib_name=$(echo ${perlbrew_lib} | cut -d @ -f2)
pb_lib_perl=$(echo ${perlbrew_lib} | cut -d @ -f1)
# init
#PERL_MB_OPT="--install_base ${perlbrew_home}/libs/${perlbrew_lib}"
PERL_LOCAL_LIB_ROOT="${perlbrew_home}/libs/${perlbrew_lib}"
#PERL_MM_OPT="INSTALL_BASE=${perlbrew_home}/libs/${perlbrew_lib}"
PERL5LIB="${perlbrew_home}/libs/${perlbrew_lib}/lib/perl5"
#PERLBREW_MANPATH="${perlbrew_home}/libs/${perlbrew_lib}/man:${perlbrew_root}/perls/${pb_lib_perl}/man"
PERLBREW_PATH="${perlbrew_home}/libs/${perlbrew_lib}/bin:${perlbrew_root}/bin:${perlbrew_root}/perls/${pb_lib_perl}/bin"
PERLBREW_LIB="${pb_lib_name}"
PERLBREW_PERL="${pb_lib_perl}"
PERLBREW_ROOT="${perlbrew_root}"
PERLBREW_HOME="${perlbrew_home}"
psgiapp_precmd() {
touch ${pidfile}
chown ${appowner} ${pidfile}
#export PERL_MB_OPT PERL_MM_OPT PERLBREW_MANPATH
export PERL_LOCAL_LIB_ROOT PERL5LIB PERLBREW_PATH PERLBREW_LIB PERLBREW_PERL PERLBREW_ROOT PERLBREW_HOME
}
psgiapp_start() {
echo "Starting ${name}."
#echo -n "appfile [${appfile}"
#if [ -e ${appfile} ]; then
# echo " : OK]"
#else
# echo " : NOT OK]"
#fi
#echo -n "pidfile [${pidfile}"
#if [ -e ${pidfile} ]; then
# echo " : OK]"
#else
# echo " : NOT OK]"
#fi
#echo "PERL_LOCAL_LIB_ROOT [${PERL_LOCAL_LIB_ROOT}]"
#echo "PERL5LIB [${PERL5LIB}]"
#echo "PERLBREW_PATH [${PERLBREW_PATH}]"
#echo "PERLBREW_LIB [${PERLBREW_LIB}]"
#echo "PERLBREW_PERL [${PERLBREW_PERL}]"
#echo "PERLBREW_ROOT [${PERLBREW_ROOT}]"
#echo "PERLBREW_HOME [${PERLBREW_HOME}]"
#echo "args [${command_args}]"
exec ${command} ${command_args}
}
psgiapp_stop() {
rc_pid=$(psgiapp_check_pidfile ${pidfile})
if [ -z "${rc_pid}" ]; then
[ -n "${rc_fast}" ] && return 0
echo "${name} not running? (check ${pidfile})."
return 1
fi
echo "Stopping ${name}."
kill -TERM ${rc_pid} 2> /dev/null
rm -f ${pidfile}
}
psgiapp_status() {
rc_pid=$(psgiapp_check_pidfile ${pidfile})
if [ -n "${rc_pid}" ]; then
echo "${name} is running as pid ${rc_pid}."
else
echo "${name} is not running."
return 1
fi
}
psgiapp_check_pidfile() {
_pidfile=$1
if [ -z "${_pidfile}" ]; then
err 3 'USAGE: psgiapp_check_pidfile pidfile'
fi
if [ ! -f ${_pidfile} ]; then
debug "pid file (${_pidfile}): not readable."
return
fi
read _pid _junk < ${_pidfile}
if [ -z "${_pid}" ]; then
debug "pid file (${_pidfile}): no pid in file."
return
fi
_proc_found=$(/bin/ps -ax -o command,pid | /usr/bin/grep -e "^starman master - [ ]\{0,\}${_pid}")
if [ -n "${_proc_found}" ]; then
echo -n ${_pid}
fi
}
psgi_command="${perlbrew_home}/libs/${perlbrew_lib}/bin/starman ${listen} ${appflags} ${appfile}"
command="/usr/sbin/daemon"
command_args="-p ${pidfile} -u ${appowner} ${psgi_command}"
run_rc_command "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment