0

I've got this zsh script. Is there a more a graceful way to do this?

export PROCESSES_TO_QUIT='puma rake sidekiq spring rails ruby-debug phantomjs zeus passenger guard resque "node server.js"'

pgr() {
  echo "Finding processes in list: $PROCESSES_TO_QUIT"  
  for x in `echo ${PROCESSES_TO_QUIT}`; do 
    pgrep -fl $x;
  done 
}

pgk() {
  echo "Killing processes in list: $PROCESSES_TO_QUIT"  
  for x in `echo ${PROCESSES_TO_QUIT}`; do 
    pkill -fl $x;
  done 
}

pgkk() {
  echo "Killing -9 processes in list: $PROCESSES_TO_QUIT"  
  for x in `echo ${PROCESSES_TO_QUIT}`; do 
    pkill -9 -fl $x;
  done 
}
3

You can use xargs to separate them and execute killall for each one :

echo $PROCESSES_TO_QUIT | xargs killall -9

| improve this answer | |
1

Maybe try killall:

% export PROCESSES_TO_QUIT='puma rake ...'
% export KILL_SIGNAL='killall -9 '
% eval $KILL_SIGNAL $PROCESSES_TO_QUIT
| improve this answer | |
  • @G-Man, the edits you made do not work for zsh on my system: e.g., processname1, processname2: no process found. Let (AT)justingordon to evaluate the suggestion in this form. – user1717828 May 3 '15 at 13:58
  • Why killall and not pkill? I'm not sure that my list would match the process names automatically. Might, but might not. – justingordon May 3 '15 at 16:24
  • I'm pretty sure killall is just pkill+grep combined. Different strokes for different folks, but with the right options they get the same things done. – user1717828 May 3 '15 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.