1
0
This repository has been archived on 2025-01-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Anderson Luiz Alves 0464e230c1 stock 103961
2017-07-30 16:48:04 -03:00

118 lines
1.9 KiB
Bash
Executable File

#!/bin/sh
# vim: set ts=2 sw=2 et:
# /etc/bewan/lib/process
[ ${PROCESS_LIB_LOADED:-0} = 1 ] && return
PROCESS_LIB_LOADED=1
#retrieve data from file
process_read()
{
local fdata="$1"
if [ "$fdata" != '' ] && [ -f ${fdata} ] && [ -r ${fdata} ]; then
exec<${fdata}
read data
echo "$data"
fi
}
#process restart with USR1
process_restart()
{
local pid_file="$1"
local pid=$(process_read ${pid_file})
#SIGUSR1
local sig="16"
if [ "$pid" != "" ]; then
kill -$sig $pid
fi
}
#soft kill and hard kill if unsuccessful after a certain time
process_stop()
{
local name="$1" time=$2 pid_file="$3"
if [ "$name" = '' ]; then
return
fi
#remove daemon
base_kill_daemon ${name} ""
base_call_initd 'inittab'
#get pid
local process_pid=$(process_read ${pid_file})
if [ "$process_pid" = '' ]; then
process_pid=$(pidof ${name})
fi
#kill daemon
if [ "$process_pid" = '' ]; then
return
fi
kill $process_pid
#set time
if [ $time -lt 0 ]; then
time=0
fi
# 'time' seconds to complete its soft kill - daemonize to avoid blocking other scripts
N=0
(
while [[ $N -lt $time ]] ; do
if ! kill -0 $process_pid
then
base_log "${name} shutdown" debug
return
fi
sleep 1
N=`expr $N + 1`
done
#kill -9 if still alive
if kill -0 $process_pid
then
base_log "${name} is still running => hard KILL" debug
kill -9 ${process_pid}
fi
) &
}
process_ip_event()
{
local name=$1 lan_up=$2 lan_app=$3 action=$4
# Nothing to do during boot
[ -f /var/run/rcrunning ] && return 0
if [ "$name" = '' ] || [ "$lan_up" = '' ] || [ "$lan_app" = '' ] || [ "$action" = '' ]; then
return
fi
if [ "$lan_up" = "$lan_app" ]; then
local ARG=$action
base_call_initd "$name"
fi
}
#restart daemon if event on lan device
process_ip_up()
{
process_ip_event $1 $2 $3 restart
}
#stop daemon if event on lan device
process_ip_down()
{
process_ip_event $1 $2 $3 stop
}