将变量读取到 ps ax 脚本

reading variables to ps ax script

您好,我一直在论坛上搜索,但我似乎无法找到正确的答案。我正在尝试创建一个脚本,询问用户他们正在搜索哪个进程,然后 returns 如果进程是 运行,则为 1。

这个有效:

#!/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

使用pgrep搜索进程:

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