tools.freebsd

Yohn Y. 2026-03-09 Parent:1dff2f4f424b

2:32ed20b72c80 Go to Latest

tools.freebsd/appdaemon.sh

* Устранение ошибок показа статуса при отказе запускаемого приложения

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