Syntax error with ps command

i am trying the blow command in vain on Linux Terminal.

kill -9 `ps -eaf | grep weblogic.NodeManager | grep wls103 | awk '{print $2}'`
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

kill -9 $(ps -eaf | grep weblogic.NodeManager | grep wls103| awk '{print $2}')
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

kill -9 ps -eaf | grep weblogic.NodeManager | grep wls103 | awk '{print $2}'
bash: kill: ps: arguments must be process or job IDs

Can you please suggest what will work ?

This begs the question, what is the output of ps -eaf | grep weblogic.NodeManager | grep wls103 | awk '{print $2}' ?

...which you could reduce to ps -eaf | awk '/weblogic.NodeManager/ && /wls103/ { print $2}' anyway.

Also, why are you running kill -9? Have you attempted to give this job a chance to quit gracefully before trying the nuclear option?

1 Like

The output of ps -eaf | grep weblogic.NodeManager | grep wls103 | awk '{print $2}' is 10997 which is the pid i wish to kill -9 on purpose.

can you please suggest why my commands fails as shown in the OP Post#1

Probably because the PID you're looking to kill doesn't exist. Either that, or ps looks different on your system than the system this came from.

I repeat, why kill -9? This is usually a bad idea.

I'm pretty sure this has been suggested before, but running a command with the shell's xtrace option set will explicitly show HOW the command is called and HOW its parameters are expanded.

Specifically, your first command would have worked if your long pipe chain had actually printed a PID.

Your second command would also have worked if your long pipe chain had actually printed a PID.

The third is obviously wrong.

So the question is, what is that long pipe chain of yours doing.

The grep and awk appear in the ps list, so ps *can* find them (race condition). Escape your search expressions, so they are not matching in the arguments.
Put one character in [ ] to achieve that.
With only awk

pids=`ps -eaf | awk '/[w]eblogic.NodeManager/ && /[w]ls103/ {print $2}'`
[ -n "$pids" ] && kill $pids

Stupid question, but why are you using ps and kill in this way? Does your system not have pgrep and pkill which allow you to search for and kill respectively processes by name? Why are you not using the -o option to ps to allow you to select exactly the fields you want to grep for and guarantee that awk will pick them up?

Andrew