Shell : How to get pid given apart of its name string - linux

I've this running java process:
testuser 37126 1 0 2018 ? 01:56:24 java -jar backend76465465.jar -XX:-OmitStackTraceInFastThrow --spring.profiles.active=server
I want to dynamically get its pid based on :
a part of its name: "backend"
(which is the fix part "backend") ,
the other part : 76465465 would always change
i ve tried this :
ps -eaf | awk '$NF~"backend" {print $2}'
and this :
ps -eaf | grep java | awk '$NF~"backend" {print $2}'
but it still give me empty results .
Suggestions ?

Try pgrep , it will return the pid of the matched process :
pgrep -f backend

Have you tried to use ps aux | grep backend?

Related

Linux ps aux with grep check for specific PHP process ID

I'm trying to figure out whether a PHP process is running or not using the ps aux command and passing grep to it, for instance:
I need it to return and tell me whether a process ID on php is running or not but whenever I try the following I always seem to get a result where the result is appending 1234 at the end, what am I missing?
ps aux | grep 'php|1234'
Suggesting pgrep command instead of ps aux
pgrep -af "php"
The reason your get always one line:
php process is not matched with grep 'php|123123123'
ps aux list the grep command you submitted and the grep command match itself
maybe you meant grep -E 'php|123123123' to match php or 123123123
The solution I've come across thanks to a user above is to do:
ps aux | grep '123456' | grep 'grep' -v
Where 123456 would be the process ID

How can we find the system service name of oracle db?

when I run ps -ef it is showing something like this
oracle 42547 1 0 Mar14 ? 00:00:35 asm_pmon_+ASM
Help me to find exact service name for oracle Database.
ps -ef | grep pmon
will show you all the processes monitor processes running in the server.
In your case, it shows asm_pmon_+ASM is the name of the process, which means,
+ASM is one of your instances running.
to extract +ASM only from the text, use:
ps -ef | grep "[p]mon" | awk -F "_" '{ print $NF }'.
Another option is to use pgrep pmon instead of ps -ef | grep ..
It is pretty straightforward when you are connected to the database instead.
select sys_context('USERENV','SERVICE_NAME') from dual;

How to get specific value from command from ps aux

Running ps aux gives something along the lines of:
user 2222 0.0 0.0 24235 346323 ? s Apr08 0:00 /command value=4
In this case, I'm trying to capture the value of 'value'. I've tried
grep 'value' | ps aux | awk '{print $11}'
without success. Can anyone help out?
Assuming GNU grep
pgrep -fl 'command.*value' | grep -oP 'value=\K\S+'
run this command
ps aux |grep 'value' | awk '{print $12}'
remember when you pipe a command, the output goes from left to right.

grep a variable containing special characters

i'm new to bash scripting and i have to determine if a process is running in a linux environment.
Actually i use the follow command to do the job:
#ps -ef | awk '{print substr($0, index($0,$8))}' | grep -v grep | grep -w -F $PROCESSNAME
where
awk '{print substr($0, index($0,$8))}'
allow me to ignore UID PID PPID C STIME TTY TIME fields and
grep -v grep
allow me to ignore the row that contains the command itself. So at this point i have a list of all processes running on the system.
Finally:
grep -w -F $PROCESSNAME
read a variable which contains the name of the process that i want to check.
For what i understand the full command should return only the row that has the exact value of $PROCESSNAME
Actually this doesn't works correctly for processes that follow the pattern "[processname]", and probably also for other patterns.
For example to simplify, if i have a running process named "[vmmemctl]" and i run:
#ps -ef | grep -v grep | grep -w -F "vmmemctl]"
it actually returns a result:
#root 615 2 0 Feb26 ? 00:01:00 [vmmemctl]
but the actual process name in the command is different from the process name in the result.
What is the correct command that doesn't have this behavior?
Thank you
awk to the rescue!
ps -ef | awk '$8=="[command]"{NF=8;print}'
or
ps -ef | awk -v c="vmmemctl]" '$8==c{NF=8;print}'
note that this is for an exact match not pattern.
since this is an exact match the command can have spaces and other special chars in it (it's not pattern match but string equality). Using your variable name it will look like this
ps -ef | awk -v c="$PROCESSNAME" '$8==c{NF=8;print}'

Linux command get tomcat process id by name

Linux command line:
When i execute the following command ps -ef |grep tomcat it shows me the following process
abcapp 28119 1 0 12:53 ? 00:00:19 /usr/java/jdk1.6.0_10//bin/java -Xmx256m -Dabc.log.file=/home/app/apps/rum/logs/dev.log -Dabc.config=dev -Dlog4j.configuration=file:///home/abcapp/env/abc_env/abc_env-1.2/config/log4j-webapp.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/home/abcapp/env/tomcat/tomcat-5.5-26-rum/conf/logging.properties -Djava.endorsed.dirs=/home/abcapp/env/tomcat/tomcat-5.5-26-rum/common/endorsed -classpath :/home/abcapp/env/tomcat/tomcat-5.5-26-rum/bin/bootstrap.jar:/home/abcapp/env/tomcat/tomcat-5.5-26-rum/bin/commons-logging-api.jar -Dcatalina.base=/home/abcapp/env/tomcat/tomcat-5.5-26-rum -Dcatalina.home=/home/abcapp/env/tomcat/tomcat-5.5-26-rum -Djava.io.tmpdir=/home/abcapp/env/tomcat/tomcat-5.5-26-rum/temp org.apache.catalina.startup.Bootstrap start
but when i issue following command it shows nothing
pgrep tomcat-5.5-26-rum OR pgrep "*-rum"
can some body help me how can i get tomcat process id by its name regex for "*-rum"
Thanks in advance.
pgrep only search for the process name without the full path (in your case only java) and without arguments.
Since tomcat-5.5-26-rum is part of the latter, i'd search the pid with
ps -ef | grep tomcat-5.5-26-rum | grep java | awk ' { print $2 } '
The double grep is useful to discard the grep pids itself
Just add following line at the start of catalina.sh file
CATALINA_PID="$CATALINA_BASE"/logs/tomcat.pid
OR
CATALINA_PID=/tmp/tomcat.pid
And bounce tomcat. This will create a tomcat.pid file in the given path and put the Tomcat process pid in it.
This worked for me:
This will give the process id of current running tomcat
echo ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'

Resources