script killing a process from service status output

Hello,

can some please suggest a script, for killing the process PID. This are steps I am currently performing to kill the process.

I cant user service splunk stop, to kill these processes, because of uid and gid mismatch for splunk user.

# service splunk status
Splunk status:
splunkd is running (PID: 8909).
splunk helpers are running (PIDs: 8910).
splunkweb is running (PID: 8972).
#kill -9 8909 8910 8972
# service splunk restart

Thanks

If you're not the same UID as splunk, you can't kill it.

I cant stop the process, so I have manually kill process using kill command as root user, so from the output service splunk status, I get all the pid, I need a script to filter all those pid's, and run kill command on those process id. After that I will change the user permission on the respective directory.

Thanks,

kill `service splunk status | awk -F ": |)" '/PID/ {print $(NF-1) }'`
1 Like

I am trying to remotely execute this command to kill process ID, these is error messages I am getthing, any idea what I am missing here.

When I login to host I can run this command successfully, but not remotely.

$ ssh -t 10.12.9.28 'sudo kill -9 `/sbin/service splunk status | awk -F ": |)" '/PID/ {print $(NF-1) }'`'
-bash: NF-1: command not found
awk: cmd. line:1: fatal: cannot open file `{print' for reading (No such file or directory)
splunk: unrecognized service
usage: kill [ -s signal | -p ] [ -a ] pid ...
       kill -l [ signal ]
Connection to 10.12.9.28 closed.
$ 

Thanks,

Try using below command... Using double quotes instead of single.

ssh -t 10.12.9.28 "sudo kill -9 `/sbin/service splunk status | awk -F ": |)" '/PID/ {print $(NF-1) }'`"

Hope it works....:slight_smile:

it didnt worked.

$ ssh -t 10.12.9.29 "sudo kill -9 `/sbin/service splunk status | awk -F ": |)" '/PID/ {print $(NF-1) }'`"
splunk: unrecognized service
usage: kill [ -s signal | -p ] [ -a ] pid ...
       kill -l [ signal ]
Connection to 10.12.9.29 closed.
$ ssh 10.12.9.29
$ /sbin/service splunk status
splunk: unrecognized service
$ su - 
# service splunk status
Splunk status:
splunkd is running (PID: 8907).
splunk helpers are running (PIDs: 8908).
splunkweb is running (PID: 8970).
#

Thanks,

You both seem to be having issues figuring out how to handle shell quoting.

To pass a single-quote when using a single-quoted string, you need to close the string, escape the single-quote, then re-open the string. In short, for each ' you need to pass, use '\'' .

Example:
echo '>>'\''<<' prints >>'<< .

For the double-quoted equivlalent, simply backslash-escape the double-quote within the double-quoted string.

Finally, don't use `...` ; it's an obsolete, deprecated, and troublesome form of command substitution. Instead, use $(...) .

Regards,
Alister

You might also want to consider stashing the kill command in a script on the remote host and then just running
ssh -t X.X.X.X /path/to/myscript.sh