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