2016-03-18 85 views
0

嗨我一直在論壇上搜索,但我似乎無法得到這個權利。我正在嘗試創建一個腳本,詢問用戶正在搜索哪個進程,如果進程正在運行,則返回1。閱讀變量爲ps ax腳本

這工作:

#!/bin/bash 
SERVICE='httpd' 
if ps ax | grep -v grep | grep $SERVICE > /dev/null 
then 
echo "$SERVICE service running, everything is fine" 
else 
echo "$SERVICE is not running" 
fi 

我想要將它添加到腳本:

echo -e "please enter process name: \c" 
read word 

類似:

#!/bin/sh 
echo -e "please enter process name: \c" 
read input_variable 
if ps ax | grep -v grep | grep $varname > /dev/null 
then 
echo "$SERVICE service running, everything is fine" 
else 
echo "$SERVICE is not running" 
fi 

回答

1

使用pgrep搜索過程:

read process_name 
if pgrep "${process_name}" >/dev/null 2>&1 ; then 
    "echo ${process_name} found" 
else 
    "echo ${process_name} not found" 
fi 
+0

很感謝它得到了第5和第7行的「」 – nycjay01