ps aux | grep returns pid for itself too - bash

I am using this command to get the process ID of another command:
ps aux | grep 7000.conf | awk '{print $2}'
This will return two PIDs:
7731
22125
I only want the first one. The second is the PID for grep in the above command. Thanks in advance to any one who knows how to alter the above command to return just the first pid.
p.s. open to a new command that does the same thing

In this particular case, escaping the . to what I assume it was meant to do should work:
ps aux | grep '7000\.conf' | awk '{print $2}'
Alternatively, exclude grep:
ps aux | grep 7000.conf | grep -v grep | awk '{print $2}'

ps aux | grep "[7]000.conf" will work as well.

Related

How to feed xargs to a piped grep for a piped cat command

How to feed xargs to a piped grep for a piped cat command.
Command 1:
(Generates a grep pattern with unique PIDs for a particular date time, read from runtime.log)
cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print $4}' | awk -F'PID=' '{print $2}' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='%
The output of above command is (It's custom grep pattern):
2018/09/13 14:50.*PID=13109
2018/09/13 14:50.*PID=14575
2018/09/13 14:50.*PID=15741
Command 2:
(Reads runtime.log and fetch the appropriate lines based on the grep pattern (Ideally the grep pattern should comes from command 1))
cat runtime.log | grep '2018/09/13 14:50.*PID=13109'
The question is How to combine both Command 1 & Command 2
Below combined version of command doesn't gives the expected output (The produced output had lines having the date other than '2018/09/13 14:50')
cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print $4}' | awk -F'PID=' '{print $2}' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='% | cat runtime.log xargs grep
grep has an option -f. From man grep:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX .)
So you could use
cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print $4}' | awk -F'PID=' '{print $2}' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='% > a_temp_file
cat runtime.log | grep -f a_temp_file
The shell has a syntax that avoids having to create the temporary file. <(). From man bash:
Process Substitution
Process substitution is supported on systems that support named pipes
(FIFOs) or the /dev/fd method of naming open files. It takes the form
of <(list) or >(list). The process list is run with its input or
output connected to a FIFO or some file in /dev/fd. The name of this
file is passed as an argument to the current command as the result of
the expansion. If the >(list) form is used, writing to the file will
provide input for list. If the <(list) form is used, the file passed
as an argument should be read to obtain the output of list.
So you can combine it to:
cat runtime.log | grep -f <(cat runtime.log | grep -e '2018/09/13 14:50' | awk -F'[ ]' '{print $4}' | awk -F'PID=' '{print $2}' | sort -u | xargs -I % echo '2018/09/13 14:50.*PID='%)

How to combine output from awk and pipe into htop?

$ ps -ef | grep python | awk -F' ' '{print $2}'
9825
4470
4619
$ htop -p 9825,4470,4619
For now, I have to make two separate commands in order to watch all python processes within htop. Is there a way that I can pipe all the results from awk and feed them into htop?
If you have pgrep (you probably do):
htop -p $(pgrep python | paste -sd,)
You could avoid grep and use only awk using something like:
ps -ef | awk '/[p]ython/{print $2}'
Then you could use:
htop -p $(ps -ef | awk -v ORS=, '/[p]ython/{print $2}')
Notice the [] around the p, this is a nice trick to avoid printing the second command itself:
ps -ef | awk '/[p]ython/{print $2}'
| |
cmd 1 cmd 2
it works because awk will translate the regex [p] to say something like "match characters from [p] in this case, p only, followed by ython:
[p]ython != python

how to grep tomcat Pid in shell?

I am using RHEL. I want grep PID of tomcat process by command
ps -ef | grep tomcat | awk '{print $2}'
my output is but I want is 28693 only.
root 12854 0.0 0.0 112652 968 pts/0 S+ 01:12 0:00 grep --color=auto tomcat
root 28693 2.1 45.0 7479444 1629972 ? Sl Apr13 21:11 /usr/java/jdk1.8.0_45//bin/jav
[root#uday ~]# ps -ef | grep tomcat | awk '{print $2}'
13240
28693
Use pgrep,
pgrep tomcat
This would return only the PID of the process.
With ps
ps -ef | grep tomcat | grep -v grep | awk '{print $2}'
ps -ef | grep to[m]cat | awk '{print $2}'
See the square brackets around one of the letters in the argument to the grep command? They are a regular expression. Square brackets in a regular expression mean "any one of those letters". So basically, you are looking for to, followed by any of the letters inside the square brackets (m only, here), followed by cat. Yes, this results to the same as writing tomcat literally, but it does prevent grep from finding it's own command line, because there it won't find the string tomcat, but to[m]cat. A little trick to prevent grep from matching itself.

How to get commands and arguments from ps command

I want to parse the output of a ps -ef command in order to display the command names and their arguments. I have two options that will display the command names:
ps -ef | awk '{print $8}'
or
ps -efo comm
However, neither of these will also display the arguments. Is there a way to do this without doing
ps -ef | awk '{print $8 $9 $10 ....}'
?
You can use ps like this:
ps -eo command

Efficient way to get your IP address in shell scripts

Context:
On *nix systems, one may get the IP address of the machine in a shell script this way:
ifconfig | grep 'inet' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'
Or this way too:
ifconfig | grep 'inet' | grep -v '127.0.0.1' | awk '{print $2}' | sed 's/addr://'
Question:
Would there be a more straightforward, still portable, way to get the IP address for use in a shell script?
(my apologies to *BSD and Solaris users as the above command may not work; I could not test)
you can do it with just one awk command. No need to use too many pipes.
$ ifconfig | awk -F':' '/inet addr/&&!/127.0.0.1/{split($2,_," ");print _[1]}'
you give direct interface thereby reducing one grep.
ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'
Based on this you can use the following command
ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'
Look here at the Beej's guide to networking to obtain the list of sockets using a simple C program to print out the IP addresses using getaddrinfo(...) call. This simple C Program can be used in part of the shell script to just print out the IP addresses available to stdout which would be easier to do then rely on the ifconfig if you want to remain portable as the output of ifconfig can vary.
Hope this helps,
Best regards,
Tom.
ifconfig | grep 'broadcast\|Bcast' | awk -F ' ' {'print $2'} | head -n 1 | sed -e 's/addr://g'
May be this could help.
more /etc/hosts | grep `hostname` | awk '{print $1}'
# for bash/linux
ipaddr(){
if="${1:-eth0}"
result=$(/sbin/ip -o -4 addr show dev "${if}" | sed 's/^.*inet // ; s/\/...*$//')
printf %s "${result}"
tty -s && printf "\n"
}

Resources