Shell – How to find the PID of a particular program

processpsshell-script

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?

Best Answer

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 '\ ' ','