pipe to kill command - unix

I am trying this out in termnial:
/tmp ps x | grep -m1 firefox | cut -d' ' -f1 | kill -KILL
kill: not enough arguments
How can I pipe the pid to kill?
I tried this, didn't work
/tmp ps x | grep -m1 firefox | kill -KILL $(cut -d' ' -f1)
cut: -: Input/output error
kill: not enough arguments

You can use xargs. This reads the output of command1 and use it as the arguments to run command2:
command1 | xargs command2
In your case
ps x | grep -m1 firefox | cut -d' ' -f1 | xargs kill -KILL

Related

Commandline subtitution on ksh is not being assigned to varibale

var2=$(echo "{$1}" | grep 'Objects that are still invalid after the validation:' | cut -d : -f2 | sed 's/ //g')
echo $var2
the above commandline substitution is not working ksh, the variable is blank each time, have tried below command too
var2="$(echo "{$1}" | grep 'Objects that are still invalid after the validation:' | cut -d : -f2 | sed 's/ //g')"
var2=`echo "{$1}" | grep 'Objects that are still invalid after the validation:' | cut -d : -f2 | sed 's/ //g'`
var2=`echo "$1" | grep 'Objects that are still invalid after the validation:' | cut -d : -f2 | sed 's/ //g'`
please hep me resolve the issue. The command is being used on remote server after ssh. The commands are working on the remote server if executed directly on the server without ssh.
What is supposed to be in $1 ? First issue is that it ought to be written as either $1 or as ${1}. Writing it as {$1} is plain wrong.
Then there is the useless use of grep and cut. The following works:
var2=$(echo ${1} | sed -ne 's/ //g' -e 's/Objectsthatarestillinvalidafterthevalidation:\(.*\)/\1/p')

Running R + awk from bash

A script run well on R, but failed when it was executed using R q -e from bash.
The script that run well on R was:
R> sizes <- read.table(pipe("ls -l /tmp | awk '!/^total/ {print $5}'"))
R> summary(sizes)
The command pattern from bash followed a previous discussion, but generated error messages:
R -q -e "x <- read.table(pipe("ls -l /tmp | awk '!/^total/ {print $5}'"));summary(x)"
awk: line 1: extra ')'
awk: line 1: extra ')'
awk: line 1: syntax error at or near ;
What's wrong with the above command?
root#kali:~# uname -a
Linux kali 3.18.0-kali3-586 #1 Debian 3.18.6-1~kali2 (2015-03-02) i686 GNU/Linux
Try this
ls -l /tmp | awk '!/^total/ {print $5}' | R --slave -e 'x <- scan(file="stdin"); summary(x)'
If you're trying to get stats on all the files in a particular directory hierarchy something like this is probably better:
find /tmp -type f -exec du {} \; | awk '{print $1}' | R --slave -e 'x <- scan(file="stdin"); summary(x)'

fetch the value of number of active threads in a process

I am trying to fetch the number of threads of a process in a UNIX using command line. After going through the man page of unix command, I learnt that following command:
ps -o nlwp <pid>
returns the number of threads spawned in a process.
Whenever i executed above command in unix, it returned:
NLWP
7
Now, I want to neglect NLWP and a space before 7.
That is I am just interested in a value, as I will be using it in a script, that I am writing for unit testing?
Is it possible to fetch only value, and neglect everything(Title NLWP, space)?
You can always use the --no-headers option in ps to get rid of the headers.
In that case, use awk to just print the first value:
ps --no-headers -o nlwp <pid> | awk '{print $1}'
Or tr to remove the spaces:
ps --no-headers -o nlwp <pid> | tr -d ' '
If --no-headers is not supported in your ps version, either of these make it:
ps -o nlwp <pid> | awk 'END {print $1}'
ps -o nlwp <pid> | tail -1 | tr -d' '

problem in a shell command

i am trying the following command on the command line
ps -u `id | cut -f2 -d"=" | cut -f1 -d"("` -f | grep ppLSN | awk '{print $9}' | awk '{FS="=";print $2}' | grep KLMN | wc -l
the value of teh command is returned as 7.
but when i am putting the same command inside a script abc_sh like below
ps -u `id | cut -f2 -d"=" | cut -f1 -d"("` -f | grep ppLSN | awk '{print $9}' | awk '{FS="=";print $2}' | grep $XYZ | wc -l
and i am calling the script on the command line as abc_sh XYZ=KLMN and it does not work and returns 0
the problem is with the grep in the command grep $XYZ
could anybody please tell why this is not working?
Because your $1 variable (first argument to the script) is set to XYZ=KLMN.
Just use abc_sh KLMN and grep $1 instead of grep $XYZ.
(Assuming we are talking about bash here)
The other alternative is defining a temporary environment variable in which case you would have to call it like this: XYZ=KLMN abc_sh
EDIT:
Found what you were using, you have to use set -k (see SHELL BUILTIN COMMANDS in the BASH manual)
-k All arguments in the form of assignment statements are
placed in the environment for a command, not just those
that precede the command name.
So
vinko#parrot:~$ more abc
#!/bin/bash
echo $XYZ
vinko#parrot:~$ set -k
vinko#parrot:~$ ./abc XYZ=KLMN
KLMN
vinko#parrot:~$ set +k
vinko#parrot:~$ ./abc XYZ=KLMN
vinko#parrot:~$
So, the place where this was working probably has set -k in one of the startup scripts (bashrc or profile.)
Try any of these to set a temporary environment variable:
XYZ=KLMN abc_sh
env XYZ=KLMN abc_sh
(export XYZ=KLMN; abc_sh)
you are using so many commands chained together....
ps -u `id -u` -f | awk -v x="$XYZ" -v p="ppLSN" '$0~p{
m=split($9,a,"=")
if(a[2]~x){count++}
}
END{print count}'
Call this script:
#!/bin/ksh
ps -u $(id -u) -o args | grep $XYZ | cut -f2- -d " "
Like this:
XYZ=KLMN abc_sh

Kill respawing server running on port 3000 created by `node . > /dev/null 2> /dev/null < /dev/null &`

I'm using AWS CodeDeploy in which server running on pm2 dose not work due to explanation given here in troubleShoot documentation.
I followed the documentation and in AfterInstall script used node . > /dev/null 2> /dev/null < /dev/null & to run the node server in the background.
I've tried following ways to kill the server
fuser -k 3000/tcp
lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9
kill -9 $(lsof -t -i:3000)
but each time a new process respwans with a different PID.
How can I kill this background process and add it to the ApplicationStop script for CodeDeploy?
One of the problems with finding a pid with grep is that the grep pid will also show up as a result and can kill itself before the target, so try;
ps ax | grep node | grep -v grep
if it looks reasonable, review this;
ps ax | grep node | grep -v grep | awk '{print $1}'
then run the kill;
ps ax | grep node | grep -v grep | awk '{print $1}' | xargs kill -9
pkill is a less flexible option (no regex filtering) but if you use that be sure to use the -I flag so you don't kill anything you did not intend to.
I was able to kill using pkill node command.

Resources