#!/bin/bash
#===============================================================================
#脚本运行的格式为 basename path -r|-k -r为启动程序,-k为kill程序。
# Name : hlht.sh
# Author : nanyun
# Version : v1.0
# Date : 20120206
# Copyright : XXXXXXXXXXXXXXXXXXXXXXXXXXXX
# Description :扫描指定路径下面的可执行文件,如果没有启动就启动该程序。
#===============================================================================
ARGS=2
E_BADARGS=65
E_NOSUCHFILE=66
if [ $# -ne "$ARGS" ]
then
if [ "$1"="-h" -o "$1"="--help" ];then
echo -e "# Description :扫描指定路径下面的可执行文件启动或者是kill程序,进程启动必须是全路径启动"
echo -e "#脚本运行的格式为 basename path -r|-k -r为启动程序,-k为kill程序。"
fi
echo "Usage: `basename $0` Path -r or -k"
exit $E_BADARGS
fi
if [ ! -d $1 ];then
echo "$1: No such file or directory"
exit $E_NOSUCHFILE
fi
ScriptSelfPwd=$(cd "$(dirname "$0")"; pwd)
cd "$ScriptSelfPwd"
case "$2" in
-r|-R)
FINDEXE="find $1 -type f -perm -755"
for PROCFULLPATH in $($FINDEXE)
do
CHECKRUN=$(ps -ef |grep "$PROCFULLPATH" |grep -v "grep")
if [ -z "$CHECKRUN" ];then
EXEPATH=$(dirname "$PROCFULLPATH")
cd "$EXEPATH"
if [ `pwd`=="$EXEPATH" ];then
echo -e "nohup $PROCFULLPATH &"
nohup "$PROCFULLPATH" &
fi
fi
done
;;
-k|-K)
FINDEXE="find $1 -type f -perm -755"
for PROCFULLPATH in $($FINDEXE)
do
GETKILLPID=$(ps -ef |grep "$PROCFULLPATH" |grep -v "grep"|awk '{print $2}')
if [ -n "$GETKILLPID" ];then
echo -e "kill $PROCFULLPATH pid $GETKILLPID"
kill -9 "$GETKILLPID"
fi
done
;;
*)
echo "$0: invalid option $2"
echo "Try $0 --help for more information."
esac
cd "$ScriptSelfPwd"