简体   繁体   English

有没有办法将shell脚本作为一个整体任务运行(使用单个PID)?

[英]Is there a way to run a shell script as one whole task(with single PID)?

I have a shell script called run.sh. 我有一个名为run.sh的shell脚本。 In it, I may call other shell scripts like: 在其中,我可以调用其他shell脚本,如:

./run_1.sh
./run_2.sh
.........

If I call the script by ./run.sh, I have found actually it will invoke different tasks inside the script sequentially with different PIDs(ie, run_1.sh will be a task and run_2.sh will be another task). 如果我通过./run.sh调用脚本,我发现实际上它将使用不同的PID顺序调用脚本内的不同任务(即run_1.sh将是一个任务而run_2.sh将是另一个任务)。 This disables me to kill the whole group of tasks using one "kill" command or run the whole group of tasks all in background by running "./run.sh &". 这使我无法使用一个“kill”命令终止整组任务,或者通过运行“./run.sh&”在后台运行整组任务。 So is there a way to run the script just as one whole task? 那么有没有办法像整个任务一样运行脚本?

  • pkill can be used for killing the children of a process, using the -P option. pkill可以用于使用-P选项来杀死进程的子进程。

     pkill -P $PID 

    where $PID is the PID of the parent process. 其中$ PID是父进程的PID。

  • You can source the run_1.sh command so that it is executed in the same shell (This could cause side effects, since now all scripts will share the same scope) . 您可以sourcerun_1.sh ,使其在同一个外壳(这可能引起副作用,因为现在所有的脚本将共享相同的范围内)执行的命令。

     source run_1.sh source run_2.sh 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM