if [ ${2} == "daemon" ]; then if [ -n ${pid} ]; then echo "nnnnnnnnnnnnnnnnnnnnnnnn-n" fi if [ -z ${pid} ]; then echo "zzzzzzzzzzzzzzzzzzzzzzzz-z" fi if [ ! -z ${pid} ]; then echo "${date}${1}(pid:${pid}) is running" else nohup ${1} 1>${3} 2>&1 & echo "${date}启动进程${1}(pid:"$!")成功" fi elif [ ${2} == "restart" ]; then if [ ! -z ${pid} ]; then kill -9 ${pid} fi nohup ${1} 1>${3} 2>&1 & echo "${date}重启进程${1}(pid:"$!")成功" elif [ ${2} == "kill" ]; then if [ ! -z ${pid} ]; then kill -9 ${pid} fi echo "${date}结束进程${1}(pid:${pid})成功" fi }
输出为:
1 2 3 4 5 6 7 8 9 10 11 12
1:/usr/local/bin/python3 /search/offline/PPCityMoniter/ppcity_scripts/main.py -T web abc:mali 7537 0.0 0.2 227712 22356 pts/3 S 18:36 0:00 /usr/local/bin/python3 /search/offline/PPCityMoniter/ppcity_scripts/main.py -T web mali 7567 0.6 0.2 305592 22468 pts/3 Sl 18:36 0:37 /usr/local/bin/python3 /search/offline/PPCityMoniter/ppcity_scripts/main.py -T web pid:7537 nnnnnnnnnnnnnnnnnnnnnnnn-n [20170720 20:15:51]/usr/local/bin/python3 /search/offline/PPCityMoniter/ppcity_scripts/main.py -T web(pid:7537) is running 1:sh /search/offline/PPCityMoniter/ppcity_scripts/utils/pull_logs.sh abc: pid: nnnnnnnnnnnnnnnnnnnnnnnn-n zzzzzzzzzzzzzzzzzzzzzzzz-z [20170720 20:15:51]启动进程sh /search/offline/PPCityMoniter/ppcity_scripts/utils/pull_logs.sh(pid:13848)成功
也就是说当pid为”空”时,程序既进入了 if [ -n ${pid} ];,也进入了if [ -z ${pid} ];,有点不懂了,-n代表非空字符串,-z代表空字符串