1

I need to try and find the ProcessId of a process.

Initially I was doing:

application="/home/user/apps/appName.app"
appPid=$(pidof -x $application)

This worked fine.

However, it now turns out that the applictaion could run from a variety of locations, such as:

/home/user/apps/appName.app
/home/user/desktop/appName.app
/home/user/desktop/link to apps/appName.app

So I tried to simply do

application="appName.app"
appPid=$(pidof -x $application) 

But this didn't find any matches. I assume that pidof requires a full path to match.

How else can I get the ProcessId?


I think I need to further explain.

If I do

ps aux | grep application.app

I get two results.

user 29912 . . . . /home/user/apps/application.app
user 12345 . . . . grep application.app

If I then do

var1=`pgrep application.app`
echo $var1

the result is blank. It should be 29912.

2
  • 1
    What if you do pgrep -f application.app?
    – muru
    Jan 13, 2016 at 10:00
  • That looks like it works. cheers
    – IGGt
    Jan 13, 2016 at 10:05

2 Answers 2

2

The pgrep itself returns the process ids. Use:

     $pgrep <process_name>


     $pgrep bash
     3896
     4013
     4115

If you want the output to store in variable:

   var1=`pgrep <appname>`


  pids=`pgrep bash`
  echo $pids
  3896 4013 4115
1

I would use the following:

appPid=$(pgrep $application)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .