在bash中获取主机脚本的PID

ank*_*540 2 bash process shell-script

我有一个脚本 ( run.sh) 来调用我的应用程序脚本。在给定的时间,我可能有多次run.sh跑步。

run.sh 脚本的一般格式是,

#!/bin/bash
# Running DALTON JOB: Helix
dir=$(pwd)
    echo "-----------------------------------------------"
   export DALTON_TMPDIR=/mnt/raid0/scratch
    export OMP_NUM_THREADS=6
    source /opt/intel/compilers_and_libraries_2017.0.098/linux/bin/compilervars.sh intel64
    source /opt/intel/mkl/bin/mklvars.sh intel64
    echo "//-------process started----------------------------//"

./application.sh  -mb 14550 input.mol output.out

echo "//-------process finished----------------------------//"
Run Code Online (Sandbox Code Playgroud)

是否可以获取脚本application.sh内部的PID run.sh。(我发现它$$给出了脚本本身的 PID。)

另外,我注意到应用程序的 PID 在数值上总是大于父脚本,但可能是巧合。

cut*_*tjm 5

我相信您可能想要将类似的东西pstree -pps axjf一些额外的解析结合起来。

通知的PID如何httpdIS 30469,和每一个过程,是一个孩子httpd拥有PPID的(父进程ID) 30469。的孙子httpd将有PPID自己的父进程,这将对PPID中的httpd过程。

我没有发布它们的完整输出,因为它们相当大。以下是每个输出的示例:

user@host$ ps -axjf
PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
1     30469 30469 30469 ?           -1 Ss       0   0:46 /usr/sbin/httpd
30469 22410 22410 22410 ?           -1 Ssl      0   0:00  \_ PassengerWatchdog
22410 22413 22413 22410 ?           -1 Sl       0  23:06  |   \_ PassengerHelperAgent
22410 22418 22418 22410 ?           -1 Sl      99   0:01  |   \_ PassengerLoggingAgent
30469 22442 30469 30469 ?           -1 Sl      48   7:55  \_ (wsgi:pulp)
30469 22443 30469 30469 ?           -1 S       48   1:48  \_ /usr/sbin/httpd
30469 22445 30469 30469 ?           -1 S       48   1:55  \_ /usr/sbin/httpd
30469 22447 30469 30469 ?           -1 S       48   1:54  \_ /usr/sbin/httpd

user@host$ pstree -p
 ??httpd(30469)???PassengerWatchd(22410)???PassengerHelper(22413)???{PassengerHelpe}(22415)
        ?              ?                        ?                        ??{PassengerHelpe}(22416)
        ?              ?                        ?                        ??{PassengerHelpe}(22417)
        ?              ?                        ?                        ??{PassengerHelpe}(22420)
        ?              ?                        ?                        ??{PassengerHelpe}(22422)
        ?              ?                        ?                        ??{PassengerHelpe}(22423)
        ?              ?                        ?                        ??{PassengerHelpe}(29342)
Run Code Online (Sandbox Code Playgroud)

请注意,如果您知道可以运行的父进程树pstree -p <pid>


Jef*_*ler 5

如果您想查看application.sh它正在运行时的 PID ,那么我建议明确将其置于后台,捕获 PID,然后等待它退出:

# ...
./application.sh  -mb 14550 input.mol output.out &
app_pid=$!
echo "The application pid is: $app_pid"
wait "$app_pid"
# ...
Run Code Online (Sandbox Code Playgroud)

  • 没错。 (2认同)