gpt4 book ai didi

linux - Shell 脚本命令输出与命令行输出不同

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:04 27 4
gpt4 key购买 nike

操作系统:RHEL 7.3。我正在运行以下命令:

ps uax | grep 'elasticsearch' | grep -v grep | awk '{print $2}'

如果我在终端上运行这个命令,我会得到一个 Elasticsearch 进程的 PID。但是,如果我将相同的命令放入 shell 脚本中,如下所示:

#!/bin/bash

PID=$(ps uax | grep 'elasticsearch' | grep -v grep | awk '{print $2}')
echo $PID

我得到了几个 PID,一个比一个低。可能出了什么问题?

启动ES的完整脚本如下:

#!/bin/bash

if [ "$ES_HOME" == "" ]; then
echo "ES_HOME environment variable does not exists. Please set it to home dir of Elasticsearch and try again"
fi;

PID=$(ps aux | grep "elasticsearch" | grep -vE "start|grep" | awk '{print $2}' | xargs)
if [ "$PID" != "" ]; then
echo "Elasticsearch is already running with PID: $PID"
echo ""
exit;
fi;

echo "Starting Elasticsearch"
sh $ES_HOME/bin/elasticsearch.sh > /dev/null &

最佳答案

在您的脚本中,您在 bash shell #!/bin/bash 中运行它,只需在手动执行时检查您当前的 shell。如果不同,则相应地更改 shell。否则我看不出有什么区别。还要检查您在运行脚本时使用的用户 ID。

关于linux - Shell 脚本命令输出与命令行输出不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48906864/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com