want to print PPID and pid in bash script [closed]
In the below program, I am able to print the ppid notable to print pid,
I want to know the answer, pid is important so I ndeed to print it
#! /bin/bash
echo " a.display PTD of parent and child "
echo " b. copy the file content "
echo " enter your choice "
read ch
case $ch in
a)
echo " parent pid is $PPID"
echo " pid is $PID"
;;
b)
echo "enter a source file"
read f1
echo "enter destination file "
read f2
if [ -f $f1 ]
then
cp $f1 $f2
else
echo "file does not exist"
fi
;;
esac
if you have sudo priveleges you can get process id using pgrep. see man pgrep
....
a)
echo " parent pid is $PPID"
#you can get child id from parent process id.
pid=$(sudo pgrep --parent $PPID)
##now you have process id as pid
echo " pid is $pid"
b)...
*i don't have enough reputation to comment and answer with sudo
You can always use $$ to get the PID.
a)
echo " parent pid is $PPID"
echo " pid is $$"
;;
I believe this is what you wanted to do.