백그라운드에서 실행되는 함수의 PID를 가져옵니다.

백그라운드에서 실행되는 함수의 PID를 가져옵니다.
#!/bin/bash

function abc() # wait for some event to happen, can be terminated by other process
{
    sleep 3333 
}

echo "PID: $$"
abc &
echo "PID: $$"

함수를 검색해야 하는데 pidecho가 동일한 문자열을 인쇄합니다.

abc()이 스크립트를 종료할 생각이 없다면 스크립트를 가져와서 pid함수를 종료할 수 있습니까?

답변1

내 생각에는 두 가지 옵션이 있습니다.

$BASHPID또는$!

echo "version: $BASH_VERSION"
function abc() # wait for some event to happen, can be terminated by other process
{
          echo "inside a subshell $BASHPID" # This gives you the PID of the current instance of Bash.
          sleep 3333
}

echo "PID: $$" # (i)
abc &
echo "PID: $$" # (ii)
echo "another way $!" # This gives you the PID of the last job run in background
echo "same than (i) and (ii) $BASHPID" # This should print the same result than (i) and (ii)

sh-4.2$ ps ax|grep foo
25094 pts/13   S      0:02 vim foo.sh
25443 pts/13   S+     0:00 grep foo

sh-4.2$ ./foo.sh
version: 4.2.39(2)-release
PID: 25448
PID: 25448
another way 25449
same than (i) and (ii) 25448
inside a subshell 25449

sh-4.2$ ps ax|grep foo
25094 pts/13   S      0:02 vim foo.sh
25449 pts/13   S      0:00 /bin/bash ./foo.sh
25452 pts/13   S+     0:00 grep foo

건배,

원천:http://tldp.org/LDP/abs/html/internalvariables.html

답변2

$를 찾고 계시는 것 같아요!

function abc() # wait for some event to happen, can be terminated by other process
{
    sleep 3333 
}

echo "PID: $$"
abc &
echo "PID: $!"

관련 정보