Manipulate o/p of a Linux command

Hi All,

I am having a heck of a time.

I have runaway processes that are hanging my rpm databases, and I have to issue kill commands.

Presently, I am outputting to a tmp file and manually putting in the kill command. but would like to automate it by the process id.

So, right now I am doing a pgrep -l rpm >/tmp/rpm

the o/p is

302 rpm
305 rpm
313 rpm
with about 9,000 processes behind it.

I would like it to be kill 302 305 313, etc.

if that is not possible.

kill 302

kill 305
etc.

any help would be appreciated.

thanks
gary

Try

pgrep -l  rpm | while read pid
do
  kill $pid
done
1 Like

It is

pgrep -l rpm | while read pid cmd
...

or better

pgrep -x rpm | while read pid
...

Even better is to check for the elapsed time

ps -e -o pid= -o etime= -o fname= |
while read pid etime cmd
do
  if [ "$cmd" = "rpm" ]
  then
    case $etime in ( *-* )
      kill "$pid"
    ;;
    esac
  fi
done

The *-* matches anything with a dash; this is true if the process is more than one day old. So it will fix your emergency, but leave enough rpm processes running to remind you to do a root cause analysis plus a proper fix.