Bash Script for Automation - Command not found - bash

I have the following in my .bash_profile:
k9 () { for A in $(ps -A | grep $* | sed 's/^\([A-Za-z0-9]*\).*/\1/' ; ) ; do "kill -9 $A"; done }
The script supposed to grep the var inputed in the command line S* get the PIDs those and execute a kill -9 on each of them. But instead I am getting this:
Machine:~ mach$ k1 Chromium
-bash: kill -9 81922: command not found
-bash: kill -9 82009: command not found
-bash: kill -9 82423: command not found
-bash: kill -9 82424: command not found
-bash: kill -9 82560: command not found
-bash: kill -9 82561: command not found
-bash: kill -9 82563: command not found
-bash: kill -9 82608: command not found
-bash: kill -9 85243: command not found
-bash: kill -9 85248: command not found
-bash: kill -9 85321: command not found
Thanks in advance!

You don't need to quote the entire command.
k9 () {
for A in $(ps -A | grep $* | sed 's/^\([A-Za-z0-9]*\).*/\1/' ; ) ; do
kill -9 "$A"
done
}

Related

How to kill multiple processes on a remote server with bash

I have this very simple bash code that should kill a list of tail -f processes on a remote server.
old_tailf_pids=`ssh root#$server "ps -ef | grep 'tail -f -n +1 /opt/wd' | grep root | grep -v grep | sed -e \"s#root *\([0-9]\+\) .*#\1#g\""`
echo $old_tailf_pids
echo "Killing old tailfs..."
ssh root#$server "kill -9 $old_tailf_pids"
Output:
4007 5281 5906 8265 8823 9918 10477 11587 12213 12753 13396 13976 14558 15985 16788 18128 18762 19412 20109 21393 28924 29487 31542 32155
Killing old tailfs...
bash: line 1: 5281: command not found
bash: line 2: 5906: command not found
bash: line 3: 8265: command not found
bash: line 4: 8823: command not found
bash: line 5: 9918: command not found
...
Seems like the SSH command killed only the first pid, and then tried to 'run' the rest of the pids. Any idea why?
Thanks
As it is evident from comments below the question that variable contains newlines after each process id, you may use this xargs command in remote ssh:
ssh root#$server "xargs kill -9 <<< \"old_tailf_pids\""

Kill a process in ruby

I use this code to kill a process with a PID file:
Process.kill 15, File.read('/tmp/pidfile').to_i
But the following two examples never work, when i try:
system "kill `cat /tmp/file.pid`"
or
`kill \`cat /tmp/pidfile\``
output is:
sh: 1: kill: Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or
kill -l [exitstatus]
Is there a problem with the backstick ? because in bash this works perfectly:
kill `cat /tmp/file.pid`
The string is not being interpolated. This does not run a cat command:
system "kill `cat /tmp/file.pid`"
Instead, you could write this as:
system "kill #{`cat /tmp/file.pid`}"
However, I'm unclear why you'd choose to do this over your original (working) method.

Script to restart another script

Hello I am trying to write a script to restart other script from command line.
usage should be:
restart someotherscript.sh
cat restart
#!/bin/bash
for pids in $(ps -ef | grep $1 | grep -v grep | awk '{print $2}')
do
kill -9 $pids
done
echo test
sleep 10
$1 &
output is:
root#xxxx:/scripts# restart pricealert.sh
Killed
root#xxxx:
My restart script is killing itself.
What is wrong here? Can you please help me?
Your script is finding itself in the search results because the command you used to start the script contains the script name you're trying to kill.
You can add an if statement to fix this ($$ is the pid of the running script):
if [ "$$" != "$pids" ]; then
kill -9 $pids
fi

Cannot kill a windows process in cygwin

I can kill a windows process in Cygwin with the command:
$ echo "4128" | xargs kill -f
but cannot do it with the following command:
$ kill -f 4128
-bash: kill: f: invalid signal specification
According to documentation of kill the syntax should be correct https://cygwin.com/cygwin-ug-net/kill.html
You can find there:
$ kill -f 123
Why doesn't the second command work?
Cygwin has 2 kills:
$ type -a kill
kill is a shell builtin
kill is /usr/bin/kill
The shell builtin does not support the -f option. You need to use the kill
executable:
/bin/kill -f
Or:
env kill -f

How to suppress command output from makefile?

My make file looks like:
nstop:
#kill `cat ${APP_ROOT}/run/nginx.pid` ||:
But I still get output:
$ make nstop
cat: /run/nginx.pid: No such file or directory
/bin/sh: 1: kill: Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or
kill -l [exitstatus]
How to suppress output from command at backtick?
I have resolved that by redirecting error output into /dev/null:
nstop:
#kill `cat ${APP_ROOT}/run/nginx.pid 2>/dev/null` 2>/dev/null ||:
But, I think, there should be better solution.

Resources