tools.freebsd
2023-09-18
Child:1dff2f4f424b
tools.freebsd/appdaemon.sh
..init
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/appdaemon.sh Mon Sep 18 22:15:46 2023 +0300 1.3 @@ -0,0 +1,191 @@ 1.4 +#!/bin/sh 1.5 +# 1.6 +# $devel.a0fs.net: appdaemon - 0.5 awgur$ 1.7 +# $FreeBSD: head/www/uwsgi/files/uwsgi.in 340872 2014-01-24 00:14:07Z mat $ 1.8 +# 1.9 +# PROVIDE: appdaemon 1.10 +# REQUIRE: DAEMON 1.11 +# KEYWORD: shutdown 1.12 +# 1.13 +# Add the following lines to /etc/rc.conf to enable appdaemon: 1.14 +# 1.15 +# appdaemon_enable (bool): Set it to "YES" to enable appdaemon 1.16 +# Default is "NO". 1.17 +# appdaemon_piddir (path): Directory with pid files of apps 1.18 +# Default "/var/run/appdaemon" 1.19 +# appdaemon_pidfile (path): Set the path to the supervisor pid file 1.20 +# Default is /var/run/appdaemon/${appName}.pid. 1.21 +# appdaemon_app_pidfile (path): Set the path to the app pid file 1.22 +# Default is /var/run/appdaemon/${appName}.app.pid. 1.23 +# appdaemon_user (str): Set the UID of the process to run with 1.24 +# Default is nobody. 1.25 +# appdaemon_app (path): Application binnary. 1.26 +# Default is "" 1.27 +# appdaemon_chdir (path): Work directory of application 1.28 +# Default is "/tmp" 1.29 +# appdaemon_app_flags (str): Flags needed by binary 1.30 +# Default is "" 1.31 +# appdaemon_self (str): Binary of daemon program 1.32 +# Default is "/usr/sbin/daemon" 1.33 +# appdaemon_syslog_ident (srt): Log all output to syslog with this identity. 1.34 +# Default is "" 1.35 +# appdaemon_flags (str): Flags of daemon(8) command 1.36 +# Default is "-fr" 1.37 +# 1.38 +# If you would like to have multiple instances running, you can 1.39 +# define multiple profiles: 1.40 +# 1.41 +# appdaemon_profiles (str): Set the list of profiles 1.42 +# Default is "". 1.43 +# 1.44 +# For each profile you can then define different options (except for 1.45 +# appdaemon_enable) using the syntax appdaemon_<profile>_<option> 1.46 + 1.47 +. /etc/rc.subr 1.48 + 1.49 +name="appdaemon" 1.50 +fsname="appdaemon" 1.51 +rcvar=`set_rcvar` 1.52 +extra_commands="list" 1.53 +list_cmd="my_list" 1.54 + 1.55 +load_rc_config $name 1.56 + 1.57 +: ${appdaemon_enable:="NO"} 1.58 +: ${appdaemon_profiles:=""} 1.59 +: ${appdaemon_piddir:="/var/run/${fsname}"} 1.60 +: ${appdaemon_pidfile:="${appdaemon_piddir}/master.pid"} 1.61 +: ${appdaemon_app_pidfile:="${appdaemon_piddir}/app.pid"} 1.62 +: ${appdaemon_app_flags:=""} 1.63 +: ${appdaemon_user:="nobody"} 1.64 +: ${appdaemon_app:=""} 1.65 +: ${appdaemon_chdir:="/tmp"} 1.66 +: ${appdaemon_flags:="-f"} 1.67 +: ${appdaemon_self:="/usr/sbin/daemon"} 1.68 +: ${appdaemon_syslog_ident:=""} 1.69 +: ${_appdaemon_prefix:="/usr/local"} 1.70 +: ${_appdaemon_instance:="appdaemon"} 1.71 + 1.72 + 1.73 +is_my_profile() { 1.74 + local profile 1.75 + for profile in ${appdaemon_profiles} ; do 1.76 + if [ "$profile" = "$1" ]; then 1.77 + return 0 1.78 + fi 1.79 + done 1.80 + 1.81 + return 1 1.82 +} 1.83 + 1.84 +my_err () { 1.85 + err $1 "${_appdaemon_instance}: $2" 1.86 +} 1.87 + 1.88 +my_list () { 1.89 + if [ -z "${appdaemon_profiles}" ] ; then 1.90 + echo "No profiles defineded" 1.91 + else 1.92 + echo "Profiles:" 1.93 + for p in ${appdaemon_profiles} ; do 1.94 + echo " - $p" 1.95 + done 1.96 + fi 1.97 + echo "" 1.98 + exit 1.99 +} 1.100 + 1.101 +if ! checkyesno ${rcvar} ; then 1.102 + exit 0 1.103 +fi 1.104 + 1.105 +if [ -n "${appdaemon_profiles}" ]; then 1.106 + _process_profiles_flag="1" 1.107 + for c in ${extra_commands} ; do 1.108 + if [ "$1" = "$c" ] ; then 1.109 + _process_profiles_flag="" 1.110 + break 1.111 + fi 1.112 + done 1.113 + if [ -n "$_process_profiles_flag" ] ; then 1.114 + if [ -n "$2" ]; then 1.115 + _profile="$2" 1.116 + if ! is_my_profile $_profile; then 1.117 + my_err 1 "$0: $_profile - no such profile defined in appdaemon_profiles." 1.118 + fi 1.119 + eval appdaemon_pidfile=\${appdaemon_${_profile}_pidfile:-"${appdaemon_piddir}/${_profile}.pid"} 1.120 + eval appdaemon_app_pidfile=\${appdaemon_${_profile}_app_pidfile:-"${appdaemon_piddir}/${_profile}.app.pid"} 1.121 + eval appdaemon_user=\${appdaemon_${_profile}_user:-"${appdaemon_user}"} 1.122 + eval appdaemon_app_flags=\${appdaemon_${_profile}_app_flags:-"${appdaemon_app_flags}"} 1.123 + eval appdaemon_self=\${appdaemon_${_profile}_self:-"${appdaemon_self}"} 1.124 + eval appdaemon_app=\"\${appdaemon_${_profile}_app:-"${appdaemon_app}"}\" 1.125 + eval appdaemon_chdir=\"\${appdaemon_${_profile}_chdir:-"${appdaemon_chdir}"}\" 1.126 + eval appdaemon_flags=\"\${appdaemon_${_profile}_flags:-"${appdaemon_flags}"}\" 1.127 + eval appdaemon_syslog_ident=\"\${appdaemon_${_profile}_syslog_ident:-"${appdaemon_syslog_ident}"}\" 1.128 + _appdaemon_instance="${_profile}" 1.129 + elif [ -n "$1" ]; then 1.130 + for _profile in ${appdaemon_profiles}; do 1.131 + echo "Processing ${name} profile: ${_profile}" 1.132 + ${_appdaemon_prefix}/etc/rc.d/${fsname} $1 ${_profile} 1.133 + done 1.134 + exit 0 1.135 + fi 1.136 + fi 1.137 +fi 1.138 + 1.139 +my_start_precmd () { 1.140 + # Checks 1.141 + if [ -z "${appdaemon_app}" ] ; then 1.142 + my_err 2 "command not set" 1.143 + elif ! [ -x "${appdaemon_app}" ] ; then 1.144 + my_err 3 "Command not found or not executable: ${appdaemon_app}" 1.145 + fi 1.146 + 1.147 + if ! [ -e "${appdaemon_piddir}" ] ; then 1.148 + mkdir -p "${appdaemon_piddir}" 1.149 + fi 1.150 +} 1.151 + 1.152 +my_start_postcmd () { 1.153 + sleep 0.4 1.154 + local _app_pid=`cat "${appdaemon_app_pidfile}"` 1.155 + local _d_pid=`cat "${appdaemon_pidfile}"` 1.156 + sleep 1 1.157 + if ! [ "${_app_pid}" = "`cat "${appdaemon_app_pidfile}"`" ] ; then 1.158 + if [ "${_d_pid}" ] ; then 1.159 + kill -9 "${_d_pid}" 1.160 + fi 1.161 + my_err 10 "app not lounch. Check it" 1.162 + fi 1.163 +} 1.164 + 1.165 +my_status_postcmd () { 1.166 + local _pids="" 1.167 + if [ -f "${appdaemon_pidfile}" ] ; then 1.168 + _pids="${_pids} -p `cat "${appdaemon_pidfile}"`" 1.169 + fi 1.170 + 1.171 + if [ -f "${appdaemon_app_pidfile}" ] ; then 1.172 + _pids="${_pids} -p `cat "${appdaemon_app_pidfile}"`" 1.173 + fi 1.174 + 1.175 + if [ -n "${_pids}" ] ; then 1.176 + echo "" 1.177 + ps ux ${_pids} 1.178 + echo "" 1.179 + fi 1.180 +} 1.181 + 1.182 +command="${appdaemon_self}" 1.183 +command_args="-r ${appdaemon_flags} -P ${appdaemon_pidfile} -p ${appdaemon_app_pidfile} -u ${appdaemon_user} ${appdaemon_app} ${appdaemon_app_flags}" 1.184 + 1.185 +if [ "x${appdaemon_syslog_ident}" != "x" ] ; then 1.186 + command_args="-ST ${appdaemon_syslog_ident} ${command_args}" 1.187 +fi 1.188 + 1.189 +appdaemon_user="" 1.190 +pidfile="${appdaemon_pidfile}" 1.191 +start_precmd=my_start_precmd 1.192 +start_postcmd=my_start_postcmd 1.193 +status_postcmd=my_status_postcmd 1.194 +run_rc_command "$1" 1.195 \ No newline at end of file