求助,在shell中获取进程id,会获取到完全不存在的pid,不知道为什么

文气东来 2021-04-16 03:26:22
while getopts ":M:" opt
do
case $opt in
M)
MAINCLASS=$OPTARG
;;
?)
echo "Illegal option,please read README.md"
exit 1;;
esac
done

MAINCLASS=com.test.auto.main.AutoMain
PROJECTNAME=test_auto_main
stopByName(){
#Get project PID
# if [ $PROJECTTYPE = class ];then
# STOPNAME=$MAINCLASS
# elif [ $PROJECTTYPE = jar ] || [ $PROJECTTYPE = main ];then
# STOPNAME=$PROJECTNAME
# else
# echo line:$LINENO illegal paramter
# fi
# echo $STOPNAME
current_pid=$( ps -ef|grep $MAINCLASS |grep -v grep | awk '{print $2}' | awk -F"/" '{ print $1 }' )
echo line:$LINENO $current_pid
if [ $current_pid ];then
echo Now the application is running, close the application first
#This is the easiest way to stop a project. If there is a better way to stop a project, it can be changed
kill -9 $current_pid
fi
}

stopByName


shell执行的目的是根据我输入的字符串找到对应的进程杀死

这样执行时返回结果为
[root@localhost new]# sh deploy1.sh   -M asfasfa
line:26


但是如果我找自己运行的程序时,此时程序还未运行,但还是获取到了未知的pid
[root@localhost new]# sh deploy1.sh   -M com.test.auto.main.AutoMain
line:26 24685 24686
deploy1.sh: line 27: [: 24685: unary operator expected


执行以下命令可以看出,我的程序的确没有运行
[root@localhost new]# ps -ef|grep com.test.auto.main.AutoMain |grep -v grep | awk '{print $2}' | awk -F"/" '{ print $1 }'       
[root@localhost new]#



这是什么原因引起的呢?
...全文
154 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yolyry 2021-04-17
  • 打赏
  • 举报
回复
先去掉-M试试
文气东来 2021-04-16
  • 打赏
  • 举报
回复
while getopts ":p:o:s:hH:m:j:t:l:L:P:n:O:S:M:a:" opt
do
    case $opt in
        M)
        M=$OPTARG
        ;;
        ?)
        echo "Illegal option,please read README.md"
        exit 1;;
    esac
done

#MAINCLASS=com.test.auto.main.AutoMain
echo $M
current_pid=$( ps -ef|grep $M |grep -v grep | awk '{print $2}' | awk -F"/" '{ print $1 }' )
echo line:$LINENO $current_pid
我整理了一下代码,发现只要字符串从外面指定并生效,就一定会出现此问题
[root@xd-vdn-liveback-01 yuewendong]# sh deploy3.sh -M aasgsdgsr

aasgsdgsr

line:16 5593 5594
文气东来 2021-04-16
  • 打赏
  • 举报
回复
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧