0

I wrote a 2 line bash script file in Centos 6.8

#! /bin/sh
pid= ps -ef | grep -i 'adminserver' | grep -v grep | awk '{print $2}'
kill -9 $pid

when I run the script i get the following output:

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
  • pid=$(ps -ef | grep -i 'adminserve[r]' | awk '{print $2}') – ctrl-alt-delor Feb 6 at 10:32
3

pid= with a space after it would set the pid variable to an empty string. The rest of that line would simply execute the pipeline and output the result (probably to the terminal unless it's being redirected). Since $pid is empty, kill later complains.

To capture the output of a command, use $(...), e.g.

pid=$( ps -ef | ... )

However, it's better to use pkill for what you're attempting to do:

pkill adminserver

See the pkill manual.

I would also avoid using the KILL signal if at all possible. See e.g. "When should I not kill -9 a process?".

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.