Error in my Shell Script (at kill command) - shell

#!/bin/ksh
ssh user#hostname "ps -ef | grep java | grep dev | kill -9 `awk '{print \$2}'` && nohup java -jar application.jar --server.port=8090&"
Error:
Usage: kill [-lL] [-n signum] [-s signame] job ...
Or: kill [ options ] -l [arg ...]
Does anyone know what is causing the error?

The \ in awk print is a syntax error.
Try this:
ps -ef | grep java | grep dev | kill -9 `awk '{print $2}'`

Related

Command works in terminal but not in makefile

This is my makefile:
start:
emulator -avd Galaxy_Nexus_API_28 -writable-system &
charles &
remount:
adb -s emulator-5554 wait-for-device root
adb -s emulator-5554 wait-for-device shell mount -o rw,remount /
adb -s emulator-5554 wait-for-device shell mount -o rw,remount /sys
stop:
kill -9 $(ps -A | grep qemu-system-x86 | awk '{print $1}') &
kill -9 $(ps -A | grep charles.jar | awk '{print $1}') &
test:
ps -A | grep qemu-system-x86
Starting the android AVD and Charles works fine. Then when I run make stop --just-print I get this as output:
kill -9 &
kill -9 &
I guess this means the output of $(ps -A | grep qemu-system-x86 | awk '{print $1}') is empty. This is weird because of two reasons:
First because when I run make test I get the following (correct) output:
ps -A | grep qemu-system-x86
64434 pts/0 00:00:59 qemu-system-x86
Second, because running echo $(ps -A | grep qemu-system-x86 | awk '{print $1}') in the terminal nicely prints the correct pid of the process. So the ps | grep command works in the makefile, but the awk print addition only works in the terminal. How can this be? And how do I fix it?
For now I'm going to stick to using killall -9 java upon calling make stop but this is of course not the nicest way to handle things.
Suggesting to try simplify stop target with pkill commands.
Like this:
stop:
pkill -9 -f qemu-system-x86
pkill -9 -f charles.jar
This solution also solve the problem of killing the grep or awk command containing the target pattern instead of killing the target process command.

Problem using grep to specify process IDs for I/O priority setting

I want to set highest I/O priority using ionice for processes with specific name (farmer and harvest)
Please help check what is wrong with my script it's not working
sudo ionice -c 1 -n 0 -p $(ps -ef | grep farmer | awk '{print $2}')
sudo ionice -c 1 -n 0 -p $(ps -ef | grep harvest | awk '{print $2}')
I got the error "ionice: ioprio_set failed: No such process" even though these processes exist
Use pgrep.
sudo ionice -c 1 -n 0 -p $(pgrep farmer)
sudo ionice -c 1 -n 0 -p $(pgrep harvest)

UNIX : Storing awk output to variable not working [duplicate]

This question already has answers here:
Bash assign output to variable inside here document
(2 answers)
Quotes within HERE document in shell scripts
(1 answer)
How to pass local variable to remote using ssh and bash script?
(3 answers)
Closed 2 years ago.
I know there are too many question regarding saving awk output to variable available on stack overflow, but I've tried all the possible answers but non seems to be working. Please help me with the following piece of code.
I've attemped the following types of solutions, but all of them give me blank output when echo.
Case 1:
sshpass -p$password ssh -T -o StrictHostKeyChecking=no $hostname <<EOF
tomcat=`ps -ef | grep -i tomcat | grep -i bootstrap | awk '{print \$2}' `
httpd=`systemctl status httpd | awk 'NR==3 {print \$2}'`
echo $tomcat
echo $httpd
EOF
Case 2:
sshpass -p$password ssh -T -o StrictHostKeyChecking=no $hostname <<EOF
tomcat=$(ps -ef | grep -i tomcat | grep -i bootstrap | awk '{print \$2}')
httpd=$(systemctl status httpd | awk 'NR==3 {print \$2}')
echo $tomcat
echo $httpd
EOF
Case 3:
sshpass -p$password ssh -T -o StrictHostKeyChecking=no $hostname <<EOF
tomcat=`ps -ef | grep -i tomcat | grep -i bootstrap | awk '{print $2}' `
httpd=`systemctl status httpd | awk 'NR==3 {print $2}'`
echo $tomcat
echo $httpd
EOF
Case 4:
sshpass -p$password ssh -T -o StrictHostKeyChecking=no $hostname <<EOF
tomcat=$(ps -ef | grep -i tomcat | grep -i bootstrap | awk '{print $2}')
httpd=$(systemctl status httpd | awk 'NR==3 {print $2}')
echo $tomcat
echo $httpd
EOF
Please help me out.
Thanks,
Sid

bash script to kill process on remote machines

I want to kill process on remote machines via ssh but its not working
VAR=$(ssh ${HOSTS} ps -ef | grep $SERVICE | grep -v grep | awk '{print $2}' | xargs kill -9)
ssh ${HOSTS} ps ef < /dev/null > /dev/null 2> /dev/null
The problem is that your pipe process get execute on your local host rather than on the server.
A solution is to quote protect the command:
VAR=$(ssh ${HOSTS} "ps -ef | grep $SERVICE | grep -v grep | awk '{print \$2}' | xargs kill -9")
ssh ${HOSTS} "ps ef" < /dev/null > /dev/null 2> /dev/null
Below command worked well for me to kill processes on remove server.
I'm able to kill tail command running on remote server.
ssh -tty ${Host}" ps -efwww | grep tail |grep -v grep |cut -c 10-15|xargs kill -9 "

Killing Process on Remote Host using Fabric

I am writing a script using Fabric which needs to terminate a process remotely.
(this means that the command ends up getting executed as /bin/bash command)
The current code I have is the following:
in a kill.sh file i have
/bin/kill $(ps -ef | grep multiserver.jar | grep -v bin/sh | grep -v /bin/bash | grep -v sh | grep python | grep -v /usr/bin/java | grep -v /usr/bin/python | grep -v sh | awk '{print $2}')
which I run in Fabric on my remote host using the following commands
local("scp " + "kill.sh " + user +"#" + server_address + ":" + directory)
run ("chmod u+x kill.sh")
run("./kill.sh")
However I get the following error message
out: Usage:
[] out: kill [options] <pid> [...]
Fatal error: run() received nonzero return code 1 while executing!
Requested: ./kill.sh
Executed: /bin/bash -l -c "cd ... && ./kill.sh"
Does anyone know what I am doing wrong?
While solving this issue with reading logs with fabric, I wrote command to kill remote processes:
with settings(warn_only=True):
sudo("kill -9 `ps aux | <pipeline of greps> | awk '{print $2}'`")
Hope this helps.

Resources