5

I would like to write a Bash script that searches for PIDs matching a program's name (perhaps using ps ax | grep <PROGRAM> or something similar) and feeds them to pixelb's ps_mem script. ps_mem needs a list of PIDs separated by commas (no spaces) in order to evaluate RAM usage, unfortunately the only way to search for processes by program name that I am aware of is ps ax | grep <PROGRAM> which returns something like (taking the example of GitHub's Atom text editor):

 7365 pts/2    S      0:00 /bin/bash /usr/bin/atom /home/fusion809/GitHub/fusion809.github.io
 7367 pts/2    Sl     2:09 /usr/share/atom/atom --executed-from=/home/fusion809/GitHub/fusion809.github.io --pid=7354 /home/fusion809/GitHub/fusion809.github.io
 7369 pts/2    S      0:00 /usr/share/atom/atom --type=zygote --no-sandbox
 7404 pts/2    Sl    69:11 /usr/share/atom/atom --type=renderer --js-flags=--harmony --no-sandbox --lang=en-GB --node-integration=true --enable-delegated-renderer --num-raster-threads=2 --gpu-rasterization-msaa-sample-count=8 --content-image-texture-target=3553 --video-image-texture-target=3553 --disable-accelerated-video-decode --disable-webrtc-hw-encoding --disable-gpu-compositing --channel=7367.0.1287479693 --v8-natives-passed-by-fd --v8-snapshot-passed-by-fd
 7469 pts/2    S      0:02 /usr/share/atom/atom --eval require('/usr/share/atom/resources/app.asar/src/compile-cache.js').setCacheDirectory('/home/fusion809/.atom/compile-cache'); require('/usr/share/atom/resources/app.asar/src/task-bootstrap.js');
10094 pts/2    Sl     0:31 /usr/share/atom/atom --type=renderer --js-flags=--harmony --no-sandbox --lang=en-GB --node-integration=true --enable-delegated-renderer --num-raster-threads=2 --gpu-rasterization-msaa-sample-count=8 --content-image-texture-target=3553 --video-image-texture-target=3553 --disable-accelerated-video-decode --disable-webrtc-hw-encoding --disable-gpu-compositing --channel=7367.1.769162379 --v8-natives-passed-by-fd --v8-snapshot-passed-by-fd
11799 pts/2    S      0:01 /usr/share/atom/atom --eval require('/usr/share/atom/resources/app.asar/src/compile-cache.js').setCacheDirectory('/home/fusion809/.atom/compile-cache'); require('/usr/share/atom/resources/app.asar/src/task-bootstrap.js');
18686 pts/2    Sl     0:02 /usr/share/atom/atom --eval require('/usr/share/atom/resources/app.asar/src/compile-cache.js').setCacheDirectory('/home/fusion809/.atom/compile-cache'); require('/usr/share/atom/resources/app.asar/src/task-bootstrap.js');
31761 pts/6    S+     0:00 grep --colour=auto atom

which as you can see is far from the syntax that ps_mem accepts. Is there a way to extract PIDs from this output in a Bash script or is there a way to otherwise get the PIDs for a specified program in a Bash script in a format that is acceptable to ps_mem?

Josh Pinto
  • 3,493

2 Answers2

5

Pidof command returns the PID and introduced by a given process name.

According entiend, you want to get a list of PIDs separated by commas, corresponding to potential PIDs as a process name. Pidof command to get that but in a list of PIDs separated by spaces. With the help of the tr command can truncate the characters corresponding to the space delimited by said output pidof another character, in this case the comma command. It could do so:

pidof <process_name> | tr '\ ' ',' 
1

What about pgrep and tr? Not sure if the last comma is a problem or not.

$ pgrep chrome | tr '\n' ,
1960,1981,1982,1984,1987,2008,2047,2079,2103,2107,2117,2123,2219,4132,4559,
  • It would be a problem I'm afraid, in fact for me pgrep cinnamon | tr '\n' , returns: 5410,5441,5459,5481,5549,5737,%, while Juan's answer returns 5481. His answer is higher stringency (exact match for the keyword [in this case cinnamon] only, not including processes with the keyword somewhere in their name like cinnamon-screensaver, for example), although I am interested in a lower stringency answer too, so if you can modify your answer to remove the final comma and %, I'll be more than interested... – Josh Pinto Dec 26 '15 at 10:14
  • pgrep doesn't output %, I bet that's your shell prompt. pgrep -d, uses comma without an added tr and omits the trailing one. pgrep -x does 'exact' (full) match. – dave_thompson_085 Dec 26 '15 at 11:55