grep the result of some Command(eg. netstat -apn) - linux

When you want to know which port was already in use you can use netstat -apn | grep 27777. The result is below:
> tcp 0 0 ::ffff:43.69.96.68:27777 :::* LISTEN 79339/java
Then you can find which process use the PID 79339.
ps -aux | grep 79339
Now I wonder if there is a command to find which process uses the port that was already in use.
I tried ps -aux | grep netstat -apn | grep 18888 | awk -F '[ /]+' '{print $7}'
but it is no working.

First run netstat and print out pid like you almost did. Then run ps and grep pid, using "word" option to avoid grepping parts of the digits (if pid is 456 you don't want to match 14567)
Put that in a bash script and you're done.
pid=$(netstat -apn | awk -F '[ /]+' '{print $7}')
ps -aux | grep -w $pid

Related

Linux use grep command

I know use ps -ef | grep test| grep -v grep |wc -l can list the num of process test,and now i plan to list the test processes belong to user :forme.is this right as below :
ps -ef | grep test|grep -x forme| grep -v grep |wc -l
For a start, grep test| grep -v grep can be replaced with grep '[t]est'. See here for an explanation.
Secondly, if you want to limit the processes to a single user, that's what the -u option to ps is for:
ps -fu forme | grep '[t]est' | wc -l
And, finally, grep already has a -c option to count lines, so you can ditch the wc part of the pipeline:
ps -fu forme | grep -c '[t]est'

alias in .cshrc to kill processes

I have a bunch of 'abcd' processes that I want to kill and restart frequently. I do this to kill them
kill -9 `ps -ef | grep abcd | grep -v grep | awk '{print $2}'`
Because I do it so frequently, I want to create an alias in the .cshrc file.
alias killabcd 'kill -9 `ps -ef | grep abcd | grep -v grep | awk '{print $2}'`'
But it seems like $2 of awk is getting expanded and I get this error when I open a terminal
Missing }.
What am I doing wrong? How can I create an alias for this in my cshrc?
Simply with pkill command:
alias killabcd 'pkill abcd'
Nevermind! I figured it out!
This is one of the ways to do it:
alias killabcd 'kill -9 `ps -ef | grep abcd | grep -v grep | awk \{print\ \$2\}`'

Killing a PID which is using a port

I am using below code to kill a process which is using a port number
port = sudo lsof -n -i4TCP:3030 | grep LISTEN | awk '{print $2;}'
if [ ! -z "$port" -a "$port" != " " ]; then
sudo kill "$port"
fi
But it is saying port: command not found. What is causing the issue and how can I fix it.
As it stands,
port = sudo lsof -n -i4TCP:3030 | grep LISTEN | awk '{print $2;}'
attempts to run a command port with parameters = sudo lsof -n -i4TCP:3030 and pipe its output through grep LISTEN and then awk '{print $2;}'.
Use
port=$(sudo lsof -n -i4TCP:3030 | grep LISTEN | awk '{print $2;}')
There's no reason to roll this yourself: fuser on Linux will do it for you in a single command, and much more efficiently:
sudo fuser -n tcp -k 3030
With just one line!
sudo kill `sudo lsof -t -i:3030`

remote ssh command not working properly

The following local command on host xyz provides the following correct output
taskset -p `ps -ef | grep ripit | grep -v grep| awk '{print \$2}'`
pid 21352's current affinity mask: 1
When I run the following command and ssh to xyz host I also get correct output
ssh xyz "ps -ef | grep ripit | grep -v grep |awk '{print \$2}'"
21352
However When I add the taskset command and run remotely on host xyz host i get this incorrect output.
ssh xyz "taskset -p `ps -ef | grep ripit | grep -v grep | awk '{print \$2}'`"
sched_getaffinity: No such process
failed to get pid 27599's affinity
bash: line 1: 32127: command not found
I tried many different single and double quote combination and I used escape character all over the place to no avail. Can anyone help?
Thanks
I haven't tested with your exact commands, but
ssh host 'lsof -p $(pgrep program)'
worked for me
For running commands remotely:
#!/bin/bash
SCRIPT='
#Your commands
'
sshpass -p<pass> ssh -o 'StrictHostKeyChecking no' -p <port> user#host "$SCRIPT"
When I add the taskset command and run remotely on host xyz host
ssh xyz "taskset -p `ps -ef | grep ripit | grep -v grep | awk '{print \$2}'`"
Here, the command substitution between `` is executed on the local host and yields a local process ID - no wonder that there is No such process on the remote host. If you escape the backquotes like
ssh xyz "taskset -p \`ps -ef | grep ripit | grep -v grep | awk '{print \$2}'\`"
the command substitution is executed on the remote host and yields the correct process ID.

How to continue executing the next other commands after using "kill" in shell?

I want to merge two different script files into one script file which could do what the two different files do. And the script files is:
script file A:
pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
kill -9 ${pid}
script file B:
nohup ./temp_tool &
the merged script file:
pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
kill -9 ${pid}
nohup ./temp_tool &
The whole merged script file would stop after executing kill command, and I have to modify it to be:
pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
out=`kill -9 ${pid}`
nohup ./temp_tool &
and it works well now, but I don't know why? Is there any difference?
I would say $pid also contains the pid of your script. You can filter it out:
script_pid=$$
pid=$(ps -ef | grep temp_tool | grep -Ev "grep|$script_pid" | awk '{print $2}')
Though if you want the pids of the command temp_tool I would suggest this:
ps -C temp_tool -o pid
Instead of the ps -ef | grep ...

Resources