shell脚本踩坑一

这种坑出现再什么情况?

python,ruby,java等 远程登录到服务器上,执行shell脚本时。

你会发现,python,ruby等远程登录执行时,脚本中的有的命令始终不会生效,但是通过终端接入,手动执行该脚本,脚本中的命令又是正常的。

比如:shell片段

while read line
do
    #统计进程的数量
    count=`ps aux | grep $line | grep -v grep | wc -l`
    if [[ $count -gt 0 ]];then
        #获取进程的状态,然后进行状态的判定
        stat=`ps aux | grep $line | grep -v grep | awk '{print $8}'`
        if [[ $stat =~ [DdTtZz] ]];then
            echo `ps aux | grep $line | grep -v grep`
            echo "$line is error state"
        else
            echo "check $line is ok"
        fi
        continue
    else
        echo "no find ${line} process"
    fi
done < vmpprocesslist.txt

执行脚本

#进程检查
/root/processcheck.sh > /root/processcheckresult

手动执行: /root/processcheck.sh  完全正常,/root/processcheckresult 中有结果,但是python,ruby,等远程调用ssh执行时,/root/processcheckresult 没有数据,甚至没有生成该文件。

很迷惑,于是仔细检查对比了一上午,终于在细节出发现了猫腻。

像这种相对路径的写法,手动执行是没有问题的,但是远程调用就一定会出现问题! 修改为/root/vmpprocesslist.txt全路径后,远程调用正常

坑:

python,ruby等远程调用shell脚本或者命令时候,一定要注意脚本中的相对位置引用,建议shell脚本中写全路径即可。

猜你喜欢

转载自blog.csdn.net/qq1119278087/article/details/107043230
今日推荐