LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2017, 06:23 AM   #1
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Rep: Reputation: 30
Listing PIDs and locations in bash


I want to script out a list of applications to show their process IDs and where they're running from.
E.g.
2504 /usr/bin/bash
2605 /usr/sbin/bash

I'd like to do something like:

ps -C name -o pid cmd

but the -o parameter can only take one argument. Is there any way to do this in one command or would I have to get a list of matching PIDs and iterate through them to get the locations? While doable it's a lot more complex.
 
Old 07-19-2017, 06:50 AM   #2
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Rep: Reputation: 95
Use this

Code:
for ProcessID in $( find /proc -maxdepth 1 -type d -name '[[:digit:]]*' -printf '%f ' ) ; do echo $ProcessID $( cat /proc/$ProcessID/cmdline ) ; done
 
1 members found this post helpful.
Old 07-19-2017, 06:57 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,876
Blog Entries: 13

Rep: Reputation: 4929Reputation: 4929Reputation: 4929Reputation: 4929Reputation: 4929Reputation: 4929Reputation: 4929Reputation: 4929Reputation: 4929Reputation: 4929Reputation: 4929
I tend to do stuff brute force a lot, and I'm not a big "set iterator", but will do that when it needs to be done.

How are you obtaining your list of PID and CMD's? Put them into a set and you can write a bash for loop to iterate on that set.

Not exact, but the syntax is something like, "for A in $SET", suggest you look up the exact syntax if you choose to do this.

As far as how to construct the set, which is called an associative array, if you do a find command or assign the output of some other type of search, it will create the associative array, what I call a set.

EDIT: Something like what tshikose wrote.
 
Old 07-19-2017, 10:41 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,847
Blog Entries: 1

Rep: Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866Reputation: 1866
ps -C name -o pid,cmd
 
2 members found this post helpful.
Old 07-19-2017, 12:31 PM   #5
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Rep: Reputation: 95
I assumed with the OP that the -o option takes just one parameter.
Obviously, NevemTeve made me wrong.
His solution is more elegant and shorter than mine.

You will just need to loop over application to substitute for name.
 
Old 06-22-2018, 04:17 AM   #6
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Rep: Reputation: 95
Sorry to revive an old thread, I just wanted to post a better and more elegant solution, while revisting my CP.

Code:
ps -C $( find /proc -maxdepth 1 -type d -name '[[:digit:]]*' -exec cat {}/comm \; | xargs | tr ' ' ',' ) -o pid,cmd
 
Old 06-22-2018, 05:54 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,629

Rep: Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265
you ought to write a small script:
Code:
find /proc ... -exec script {} \;
and the script will do the ps
 
Old 06-22-2018, 01:32 PM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
ps -C name -o pid,cmd
compare with
Code:
lsof -i : 2504 ; lsof -i : 2605
Check a directory for "open processes" using:
Code:
lsof +D /var/www/html/
simplest method (for me personally) is
Code:
lsof |grep <keyword>
and look for /path/to/bin or /some/curious/file

See also http://blog.commandlinekungfu.com/2010/11/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursively listing files using bash builtins Neville Hillyer *BSD 3 01-21-2016 12:43 AM
bash case like listing for perl casperdaghost Programming 1 10-09-2013 12:43 AM
Bash Shell Script - Listing files RML1992 Programming 2 09-14-2012 02:09 PM
Bash - listing directories tarja Programming 8 12-03-2005 04:03 PM
Bash Scripts for Dir listing jchristman Linux - General 6 08-26-2004 08:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration