tools.freebsd

Yohn Y. 2023-09-18 Child:1dff2f4f424b

0:60183461e11f Go to Latest

tools.freebsd/appdaemon.sh

..init

History
1 #!/bin/sh
2 #
3 # $devel.a0fs.net: appdaemon - 0.5 awgur$
4 # $FreeBSD: head/www/uwsgi/files/uwsgi.in 340872 2014-01-24 00:14:07Z mat $
5 #
6 # PROVIDE: appdaemon
7 # REQUIRE: DAEMON
8 # KEYWORD: shutdown
9 #
10 # Add the following lines to /etc/rc.conf to enable appdaemon:
11 #
12 # appdaemon_enable (bool): Set it to "YES" to enable appdaemon
13 # Default is "NO".
14 # appdaemon_piddir (path): Directory with pid files of apps
15 # Default "/var/run/appdaemon"
16 # appdaemon_pidfile (path): Set the path to the supervisor pid file
17 # Default is /var/run/appdaemon/${appName}.pid.
18 # appdaemon_app_pidfile (path): Set the path to the app pid file
19 # Default is /var/run/appdaemon/${appName}.app.pid.
20 # appdaemon_user (str): Set the UID of the process to run with
21 # Default is nobody.
22 # appdaemon_app (path): Application binnary.
23 # Default is ""
24 # appdaemon_chdir (path): Work directory of application
25 # Default is "/tmp"
26 # appdaemon_app_flags (str): Flags needed by binary
27 # Default is ""
28 # appdaemon_self (str): Binary of daemon program
29 # Default is "/usr/sbin/daemon"
30 # appdaemon_syslog_ident (srt): Log all output to syslog with this identity.
31 # Default is ""
32 # appdaemon_flags (str): Flags of daemon(8) command
33 # Default is "-fr"
34 #
35 # If you would like to have multiple instances running, you can
36 # define multiple profiles:
37 #
38 # appdaemon_profiles (str): Set the list of profiles
39 # Default is "".
40 #
41 # For each profile you can then define different options (except for
42 # appdaemon_enable) using the syntax appdaemon_<profile>_<option>
44 . /etc/rc.subr
46 name="appdaemon"
47 fsname="appdaemon"
48 rcvar=`set_rcvar`
49 extra_commands="list"
50 list_cmd="my_list"
52 load_rc_config $name
54 : ${appdaemon_enable:="NO"}
55 : ${appdaemon_profiles:=""}
56 : ${appdaemon_piddir:="/var/run/${fsname}"}
57 : ${appdaemon_pidfile:="${appdaemon_piddir}/master.pid"}
58 : ${appdaemon_app_pidfile:="${appdaemon_piddir}/app.pid"}
59 : ${appdaemon_app_flags:=""}
60 : ${appdaemon_user:="nobody"}
61 : ${appdaemon_app:=""}
62 : ${appdaemon_chdir:="/tmp"}
63 : ${appdaemon_flags:="-f"}
64 : ${appdaemon_self:="/usr/sbin/daemon"}
65 : ${appdaemon_syslog_ident:=""}
66 : ${_appdaemon_prefix:="/usr/local"}
67 : ${_appdaemon_instance:="appdaemon"}
70 is_my_profile() {
71 local profile
72 for profile in ${appdaemon_profiles} ; do
73 if [ "$profile" = "$1" ]; then
74 return 0
75 fi
76 done
78 return 1
79 }
81 my_err () {
82 err $1 "${_appdaemon_instance}: $2"
83 }
85 my_list () {
86 if [ -z "${appdaemon_profiles}" ] ; then
87 echo "No profiles defineded"
88 else
89 echo "Profiles:"
90 for p in ${appdaemon_profiles} ; do
91 echo " - $p"
92 done
93 fi
94 echo ""
95 exit
96 }
98 if ! checkyesno ${rcvar} ; then
99 exit 0
100 fi
102 if [ -n "${appdaemon_profiles}" ]; then
103 _process_profiles_flag="1"
104 for c in ${extra_commands} ; do
105 if [ "$1" = "$c" ] ; then
106 _process_profiles_flag=""
107 break
108 fi
109 done
110 if [ -n "$_process_profiles_flag" ] ; then
111 if [ -n "$2" ]; then
112 _profile="$2"
113 if ! is_my_profile $_profile; then
114 my_err 1 "$0: $_profile - no such profile defined in appdaemon_profiles."
115 fi
116 eval appdaemon_pidfile=\${appdaemon_${_profile}_pidfile:-"${appdaemon_piddir}/${_profile}.pid"}
117 eval appdaemon_app_pidfile=\${appdaemon_${_profile}_app_pidfile:-"${appdaemon_piddir}/${_profile}.app.pid"}
118 eval appdaemon_user=\${appdaemon_${_profile}_user:-"${appdaemon_user}"}
119 eval appdaemon_app_flags=\${appdaemon_${_profile}_app_flags:-"${appdaemon_app_flags}"}
120 eval appdaemon_self=\${appdaemon_${_profile}_self:-"${appdaemon_self}"}
121 eval appdaemon_app=\"\${appdaemon_${_profile}_app:-"${appdaemon_app}"}\"
122 eval appdaemon_chdir=\"\${appdaemon_${_profile}_chdir:-"${appdaemon_chdir}"}\"
123 eval appdaemon_flags=\"\${appdaemon_${_profile}_flags:-"${appdaemon_flags}"}\"
124 eval appdaemon_syslog_ident=\"\${appdaemon_${_profile}_syslog_ident:-"${appdaemon_syslog_ident}"}\"
125 _appdaemon_instance="${_profile}"
126 elif [ -n "$1" ]; then
127 for _profile in ${appdaemon_profiles}; do
128 echo "Processing ${name} profile: ${_profile}"
129 ${_appdaemon_prefix}/etc/rc.d/${fsname} $1 ${_profile}
130 done
131 exit 0
132 fi
133 fi
134 fi
136 my_start_precmd () {
137 # Checks
138 if [ -z "${appdaemon_app}" ] ; then
139 my_err 2 "command not set"
140 elif ! [ -x "${appdaemon_app}" ] ; then
141 my_err 3 "Command not found or not executable: ${appdaemon_app}"
142 fi
144 if ! [ -e "${appdaemon_piddir}" ] ; then
145 mkdir -p "${appdaemon_piddir}"
146 fi
149 my_start_postcmd () {
150 sleep 0.4
151 local _app_pid=`cat "${appdaemon_app_pidfile}"`
152 local _d_pid=`cat "${appdaemon_pidfile}"`
153 sleep 1
154 if ! [ "${_app_pid}" = "`cat "${appdaemon_app_pidfile}"`" ] ; then
155 if [ "${_d_pid}" ] ; then
156 kill -9 "${_d_pid}"
157 fi
158 my_err 10 "app not lounch. Check it"
159 fi
162 my_status_postcmd () {
163 local _pids=""
164 if [ -f "${appdaemon_pidfile}" ] ; then
165 _pids="${_pids} -p `cat "${appdaemon_pidfile}"`"
166 fi
168 if [ -f "${appdaemon_app_pidfile}" ] ; then
169 _pids="${_pids} -p `cat "${appdaemon_app_pidfile}"`"
170 fi
172 if [ -n "${_pids}" ] ; then
173 echo ""
174 ps ux ${_pids}
175 echo ""
176 fi
179 command="${appdaemon_self}"
180 command_args="-r ${appdaemon_flags} -P ${appdaemon_pidfile} -p ${appdaemon_app_pidfile} -u ${appdaemon_user} ${appdaemon_app} ${appdaemon_app_flags}"
182 if [ "x${appdaemon_syslog_ident}" != "x" ] ; then
183 command_args="-ST ${appdaemon_syslog_ident} ${command_args}"
184 fi
186 appdaemon_user=""
187 pidfile="${appdaemon_pidfile}"
188 start_precmd=my_start_precmd
189 start_postcmd=my_start_postcmd
190 status_postcmd=my_status_postcmd
191 run_rc_command "$1"