Reputation: 4922
I have problem with Bash, and I don't know why.
Under shell, I enter:
echo $$ ## print 2433
(echo $$) ## also print 2433
(./getpid) ## print 2602
Where getpid
is a C program to get current pid, like:
int main() { printf("%d", (int)getpid()); return 0; }
What confuses me is that:
$$
something like macro?Can you help me?
Upvotes: 194
Views: 331467
Reputation: 2536
get_own_pid() {
# This function being called in a subshell,
# it must returns the pid of the parent of the "cut" command parent
cut -d' ' -f4 < /proc/self/stat \
| xargs -I% sh -c 'cut -d" " -f4 < /proc/%/stat'
}
get_parent_pid() {
# Same thing but repeating the last command once more to get the parent one level above
cut -d' ' -f4 < /proc/self/stat \
| xargs -I% sh -c 'cut -d" " -f4 < /proc/%/stat' \
| xargs -I% sh -c 'cut -d" " -f4 < /proc/%/stat'
}
# Here pid is the same as the $$ pid because called from main process
MY_PID=$(get_own_pid)
echo "$$ == ${MY_PID}"
# Here called in a subprocess, the returned pid is different
(
MY_CHILD_PID=$(get_own_pid)
PARENT_PID_FROM_CHILD=$(get_parent_pid)
echo "$$ != ${MY_CHILD_PID}"
echo "$$ == ${PARENT_PID_FROM_CHILD}"
)
Inspired from artem lapkin answer, thanks!
Upvotes: 0
Reputation: 5
if you want a simple shell script for getting the maximum PID with variable, do this
pid=$(cat /proc/sys/kernel/pid_max)
echo $pid
that will print you the maximum PID can be.
Upvotes: 0
Reputation: 49
this one univesal way to get correct pid
pid=$(cut -d' ' -f4 < /proc/self/stat)
same nice worked for sub
SUB(){
pid=$(cut -d' ' -f4 < /proc/self/stat)
echo "$$ != $pid"
}
echo "pid = $$"
(SUB)
check output
pid = 8099
8099 != 8100
Upvotes: 4
Reputation: 505
If you were asking how to get the PID of a known command it would resemble something like this:
If you had issued the command below #The command issued was ***
dd if=/dev/diskx of=/dev/disky
Then you would use:
PIDs=$(ps | grep dd | grep if | cut -b 1-5)
What happens here is it pipes all needed unique characters to a field and that field can be echoed using
echo $PIDs
Upvotes: 1
Reputation: 1411
You can use one of the following.
$!
is the PID of the last backgrounded process.kill -0 $PID
checks whether it's still running.$$
is the PID of the current shell.Upvotes: 85
Reputation: 531335
$$
is defined to return the process ID of the parent in a subshell; from the man page under "Special Parameters":
$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell.
In bash
4, you can get the process ID of the child with BASHPID
.
~ $ echo $$
17601
~ $ ( echo $$; echo $BASHPID )
17601
17634
Upvotes: 261
Reputation: 41958
$$
is an alias in Bash to the current script PID. See differences between $$
and $BASHPID
here, and right above that the additional variable $BASH_SUBSHELL
which contains the nesting level.Upvotes: 32