Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug for issue: github.com/open-falcon/agent/issues/53 #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 151 additions & 35 deletions control
Original file line number Diff line number Diff line change
@@ -1,31 +1,134 @@
#!/bin/bash

export PS4='+ [`basename ${BASH_SOURCE[0]}`:$LINENO ${FUNCNAME[0]} \D{%F %T} $$ ] '
WORKSPACE=$(cd $(dirname $0)/; pwd)
cd $WORKSPACE

mkdir -p var

module=agent
app=falcon-$module
DAEMON="$WORKSPACE/$app"
conf=cfg.json
pidfile=var/app.pid
logfile=var/app.log

function check_pid() {
if [ -f $pidfile ];then
pid=`cat $pidfile`
if [ -n $pid ]; then
running=`ps -p $pid|grep -v "PID TTY" |wc -l`
return $running
# __proc_pids {program} [pidfile]
# Set $pid to pids from /var/run* for {program}. $pid should be declared
# local in the caller.
# Returns LSB exit code for the 'status' action.
__pids_var_run() {
local base=${1##*/}
local pid_file=${2:-/var/run/$base.pid}
local pid_dir=$(/usr/bin/dirname $pid_file)
local binary=$3

[ -d "$pid_dir" -a ! -r "$pid_dir" ] && return 4

pid=
if [ -f "$pid_file" ] ; then
local line p

[ ! -r "$pid_file" ] && return 4 # "user had insufficient privilege"
while : ; do
read line
[ -z "$line" ] && break
for p in $line ; do
if [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] ; then
if [ -n "$binary" ] ; then
local b=$(readlink /proc/$p/exe | sed -e 's/\s*(deleted)$//')
[ "$b" != "$binary" ] && continue
fi
pid="$pid $p"
fi
done
done < "$pid_file"

if [ -n "$pid" ]; then
num=`cat "/proc/$p/cmdline" |grep "$base" |wc -l`
if [ $num -gt 0 ]; then
return 0
fi
pid=""
fi
return 1 # "Program is dead and /var/run pid file exists"
fi
return 0
return 3 # "Program is not running"
}

# Output PIDs of matching processes, found using pidof
__pids_pidof() {
pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || \
pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}"
}

_status() {
local base pid lock_file= pid_file= binary=

# Test syntax.
if [ "$#" = 0 ] ; then
echo $"Usage: _status [-p pidfile] {program}"
return 1
fi
if [ "$1" = "-p" ]; then
pid_file=$2
shift 2
fi
if [ "$1" = "-l" ]; then
lock_file=$2
shift 2
fi
if [ "$1" = "-b" ]; then
if [ -z $pid_file ]; then
echo $"-b option can be used only with -p"
echo $"Usage: _status -p pidfile -b binary program"
return 1
fi
binary=$2
shift 2
fi
base=${1##*/}

# First try "pidof"
__pids_var_run "$1" "$pid_file" "$binary"
RC=$?
if [ -z "$pid_file" -a -z "$pid" ]; then
pid="$(__pids_pidof "$1")"
fi
if [ -n "$pid" ]; then
return 0
fi

case "$RC" in
0)
# Program is started.
return 0
;;
1)
# Dead but pid file exists.
return 1
;;
4)
# Unknown due to insufficient privileges.
return 4
;;
esac
if [ -z "${lock_file}" ]; then
lock_file=${base}
fi
# See if /var/lock/subsys/${lock_file} exists
if [ -f /var/lock/subsys/${lock_file} ]; then
# dead but subsys locked"
return 2
fi
# Program is stopped"
return 3
}

function start() {
check_pid
_status -p $pidfile $DAEMON
running=$?
if [ $running -gt 0 ];then
if [ $running -eq 0 ];then
echo -n "$app now is running already, pid="
cat $pidfile
return 1
Expand All @@ -48,9 +151,13 @@ function start() {
}

function stop() {
pid=`cat $pidfile`
kill $pid
rm -f $pidfile
_status -p $pidfile $DAEMON
running=$?
if [ $running -eq 0 ];then
pid=`cat $pidfile`
kill $pid
rm -f $pidfile
fi
echo "$app stoped..."
}

Expand All @@ -61,15 +168,16 @@ function restart() {
}

function status() {
check_pid
_status -p $pidfile $DAEMON
running=$?
if [ $running -gt 0 ];then
if [ $running -eq 0 ];then
echo started
else
echo stoped
fi
}


function tailf() {
tail -f $logfile
}
Expand Down Expand Up @@ -103,24 +211,32 @@ function help() {
echo "$0 build|pack|start|stop|restart|status|tail"
}

if [ "$1" == "" ]; then
help
elif [ "$1" == "stop" ];then
stop
elif [ "$1" == "start" ];then
start
elif [ "$1" == "restart" ];then
restart
elif [ "$1" == "status" ];then
status
elif [ "$1" == "tail" ];then
tailf
elif [ "$1" == "build" ];then
build
elif [ "$1" == "pack" ];then
pack
elif [ "$1" == "packbin" ];then
packbin
else
help
fi
case "$1" in
stop)
stop
;;
start)
start
;;
restart)
restart
;;
status)
status
;;
tail)
tailf
;;
build)
build
;;
pack)
pack
;;
packbin)
packbin
;;
*)
help
;;
esac