0
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.

1
  • 1
    Please check the man pages of pidof and pgrep Commented Nov 21, 2016 at 6:03

2 Answers 2

2

Don't reinvent the wheel, please!

pidof <process name>
Sign up to request clarification or add additional context in comments.

5 Comments

I do think we have to find the process first?
man pidof, 1st line: "pidof -- find the process ID of a running program."
what if the the a process called $name does not actually exist?
By default ps , pgrep, pidof give info about an existing - running process. If process is not running will not be listed. Are you looking to find an loaded/running process or to view installed apps? Different story.
Don't forget the -x option to identify running scripts, as well as the program running the script.
0

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").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.