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]

1 Answers1

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?".

Kusalananda
  • 333,661