1#!/bin/sh
2#
3# $FreeBSD: assp,v 1.0 2013/11/09 08:55:56 TE Exp $
4#
5# dont't use symlinks for pathes in this script (/home/assp => /usr/home/assp) !
6#
7# PROVIDE: assp
8# KEYWORD: shutdown
9#
10#
11
12
13. /etc/rc.subr
14
15assp_enable=${assp_enable}
16
17name=assp
18rcvar="${name}_enable"
19
20start_cmd=assp_start
21stop_cmd=assp_stop
22status_cmd=assp_status
23extra_commands="status"
24
25# specify your pathes here
26#
27# the path to the assp user home
28user_home="/usr/home/${name}"
29
30# the path to the perl binary
31perlbin="${user_home}/bin/perl"
32
33# the path to the assp base folder
34base="${user_home}/${name}"
35
36# the path to the assp.pl script
37assp="${base}/${name}.pl"
38
39# the path to the assp PID file
40pidfile="${base}/pid"
41
42# tells assp that it is started as a OS controlled daemon
43assp_parms="--AsADaemon:=2"
44
45
46# code goes here
47get_status() {
48    local retcode=0 # assume assp is running
49    if [ -f ${pidfile} ]; then # is the PID found
50        if ! ps -p $(cat $pidfile) > /dev/null; then # is the process not running
51	    rm ${pidfile}
52	    echo "removed orphaned PID file ${pidfile}"
53	    retcode=1 # assp is not running
54	fi
55    else
56	retcode=1 # assp is not running
57    fi
58    return $retcode
59}
60
61assp_start() {
62    checkyesno assp_enable && ! get_status && echo "Starting ${name}." && \
63    su -m ${name} -c "${perlbin} ${assp} ${base} ${assp_parms}" > /dev/null && \
64    echo "${name} started" || \
65    echo "${name} not started"
66}
67
68assp_stop() {
69    if [ ! -f ${pidfile} ];then # PID file not available
70	echo "${name} seems not to be running - missing PID file ${pidfile}"
71	exit 1
72    fi
73    if ! get_status; then # assp is not running
74        exit 1
75    fi
76    echo "Stopping ${name} - sent INT."
77    pkill -INT -F ${pidfile} # send INT to assp
78    echo "wait for ${name} termination - max 60 seconds."
79    local timeout=60
80    # wait until assp has stopped but max 60 seconds
81    while [ -f ${pidfile} -a ${timeout} -gt 0 ]; do
82        timeout=$(( timeout -1 ))
83        sleep 1
84    done
85    if [ -f ${pidfile} ];then # assp is still running - kill it
86        echo "${name} was not stopped within 60 seconds - will KILL it"
87        pkill -KILL -F ${pidfile}
88        sleep 2
89        rm ${pidfile}
90    fi
91    echo "${name} stopped"
92}
93
94assp_status() {
95    if get_status; then
96	echo "${name} is running as pid" $(cat $pidfile).
97        return 0
98    else
99	echo $name is not running
100        return 1
101    fi
102}
103
104
105load_rc_config ${name}
106run_rc_command "$1"
107
108