faker
faker

Reputation: 5

check the existence of program with specific process name

Read=$((ps -eaf | grep "$name" | grep -v grep | awk '{print $1}'))
Read1=$((ps -p <$Read>))

if [ $Read1 -ne 0 ]; then
exit 1

This is what I get so far, I want to first get the pid of the $name, then check whether there is process corresponding to that pid. If there is not, exit 1.

I am not sure about this.

Upvotes: 0

Views: 639

Answers (2)

Jamil Said
Jamil Said

Reputation: 2093

This is what I would use for this purpose:

test="$(ps aux | grep -sie "process-name" | grep -v "grep -sie")"
if [ -z "$test" ]; then exit 1; else echo "Process found ----- $test"; fi

This code is case insensitive (ex.: it would match "process-name" but also "Process-NAME").

Upvotes: 0

Ipor Sircer
Ipor Sircer

Reputation: 3141

Don't reinvent the wheel, please!

pidof <process name>

Upvotes: 2

Related Questions