Home > database >  How to get process id of calling process if elevated via sudo
How to get process id of calling process if elevated via sudo

Time:01-28

I have a service on Ubuntu which elevate new process using sudo command and then call bash script XY.

From this bash script XY I need to find process id of the service which called the process.

Parent process id ($$) does not work, probably because sudo elevation.

CodePudding user response:

Try this test.sh

#!/bin/bash

ps -ef | awk '$2 == "'$PPID'" { print $3 }'

Run it with :

$ chmod  x test.sh
$ sudo ./test.sh
$ echo "Sudo should print $$."
  •  Tags:  
  • bash
  • Related