Many a times, one needs to get the PID of a process to do some additional tasks on it.
Here is a script which could be added into other scripts where you need such a requirement so as to kill a certain process if running, start a process if not found and many such cases!
--------------------------------------------------------------------------------------------------------------
#!/bin/bash
#This script is to be used as ./testPID <processname>
PID=$(pgrep $@)
#if [ -z "$PID" ] is same as writing if[ "$PID" = "" ]
if [ "$PID" = "" ]; then
echo "Process not found"
else
echo "Process[PID]: $@[$PID]"
fi
----------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment