How to get only the process ID for a specified process name in linux?
ps -ef|grep java
test 31372 31265 0 13:41 pts/1 00:00:00 grep java
Based on the process id I will write some logic. So how do I get only the process id for a specific process name.
Sample program:
PIDS= ps -ef|grep java
if [ -z "$PIDS" ]; then
echo "nothing"
else
mail test@domain.com
fi
You can use:
Or if
pgrep
is available then better to use:why not just pidof ?
it will return a list of pids matching the process name
https://linux.die.net/man/8/pidof
This command ignore grep process, and just return PID:
Use this:
ps -C <name> -o pid=
You can pipe your output to awk to print just the PID. For example: