我有一个启动多个进程的bash脚本调用run.sh
#!/bin/bash
proc1 &
proc2 &
proc3 &
final # this runs until sigterm
当我执行run.sh
并将SIGTERM发送到run.sh时,我认为SIGTERM不会被发送到final
,我认为它不会被发送到{{} 1}},proc1
和proc2
。请注意,在此用例中,这是一个运行proc3
的docker容器,并且正在尝试发送SIGTERM,正在运行run.sh
。
bash脚本将sigterm发送到它启动的所有进程的最简单方法是什么?我能想到的唯一方法是使用docker stop
启动final
,然后在&
中执行while循环?
编辑 - 虽然我尝试过但似乎不起作用:
在run.sh
run.sh
当停止停靠时,我从未看到#!/bin/bash
_term() {
echo "Caught SIGTERM signal!"
}
trap _term SIGTERM
echo "hi"
sleep 100000 &
wait $!
答案 0 :(得分:1)
你说你在Docker容器中运行该脚本。您能否详细介绍一下如何启动容器以及如何调用docker stop
?
当调用SIGTERM
或容器收到直接run.sh
时,包含PID 1的进程将接收它。当您的&
创建在后台中运行的子进程时,它还必须将信号转发给他们。
因此,在使用supervisord
的bash脚本中创建后台子进程不是一个好方法。使用supervisor是一种很好的做法,因为它可以正确处理信号并将它们转发到子进程,而无需进一步编写脚本。
此外,Dockerfile
不应作为shell子进程本身启动。如果您在CMD /usr/bin/supervisord
:
CMD ["/usr/bin/supervisord"]
相反它应该看起来像:
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.internet.MimeMultipart;
import org.jsoup.Jsoup;
....
private String getTextFromMessage(Message message) throws Exception {
if (message.isMimeType("text/plain")){
return message.getContent().toString();
}else if (message.isMimeType("multipart/*")) {
String result = "";
MimeMultipart mimeMultipart = (MimeMultipart)message.getContent();
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i ++){
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")){
result = result + "\n" + bodyPart.getContent();
break; //without break same text appears twice in my tests
} else if (bodyPart.isMimeType("text/html")){
String html = (String) bodyPart.getContent();
result = result + "\n" + Jsoup.parse(html).text();
}
}
return result;
}
return "";
}
这样主管就成为PID 1的根进程,并将正确接收所有信号,并将它们重定向到子进程。
答案 1 :(得分:0)
使用jobs -p
获取所有后台作业的流程ID,然后将其传递给kill
。
trap 'kill $(jobs -p)' TERM
proc1 &
proc2 &
proc3 &
wait
答案 2 :(得分:-1)
正确,我会将它们全部收集在一个数组中,然后在完成时向每个数组发送一个信号。我会使用像dReader.o Make/linux64GccDPOpt/chem1dToFoam.o -L/home/vasu/OpenFOAM/OpenFOAM-2.3.1/platforms/linux64GccDPOpt/lib \
-L/home/vasu/OpenFOAM/vasu-2.3.1/platforms/linux64GccDPOpt/lib -lODE -lspecie -lcompressibleLESModels -lcompressibleTurbulenceModel - lcompressibleRASModels -lpdfFGMbasicThermophysicalModels - lpdfFGMreactionThermophysicalModels -lpdfFGMCombustionModels - lfiniteVolume -lOpenFOAM -ldl -lm -o
/home/vasu/OpenFOAM/vasu- 2.3.1/platforms/linux64GccDPOpt/bin/pasrChem1dToFoam_m
/home/vasu/OpenFOAM/vasu-2.3.1/platforms/linux64GccDPOpt/lib/libpdfFGMbasicThermophysicalModels.so: undefined reference to `Foam::fluidThermo::destroyTimeConstructorTables()'
/home/vasu/OpenFOAM/vasu-2.3.1/platforms/linux64GccDPOpt/lib/libpdfFGMbasicThermophysicalModels.so: undefined reference to `Foam::basicThermo::constructTimeConstructorTables()'
/home/vasu/OpenFOAM/vasu-2.3.1/platforms/linux64GccDPOpt/lib/libpdfFGMbasicThermophysicalModels.so: undefined reference to `Foam::basicThermo::destroyTimeConstructorTables()'
/home/vasu/OpenFOAM/vasu-2.3.1/platforms/linux64GccDPOpt/lib/libpdfFGMbasicThermophysicalModels.so: undefined reference to `Foam::fluidThermo::fluidThermo(Foam::Time const&, Foam::word const&)'
/home/vasu/OpenFOAM/vasu-2.3.1/platforms/linux64GccDPOpt/lib/libpdfFGMbasicThermophysicalModels.so: undefined reference to `Foam::fluidThermo::constructTimeConstructorTables()'
/home/vasu/OpenFOAM/vasu-2.3.1/platforms/linux64GccDPOpt/lib/libpdfFGMbasicThermophysicalModels.so: undefined reference to `Foam::basicThermo::TimeConstructorTablePtr_'
/home/vasu/OpenFOAM/vasu-2.3.1/platforms/linux64GccDPOpt/lib/libpdfFGMbasicThermophysicalModels.so: undefined reference to `Foam::fluidThermo::TimeConstructorTablePtr_'
collect2: error: ld returned 1 exit status
/home/vasu/OpenFOAM/OpenFOAM-2.3.1/wmake/Makefile:149: recipe for target '
/home/vasu/OpenFOAM/vasu-2.3.1/platforms/linux64GccDPOpt/bin/pasrChem1dToFoam_m' failed
make: *** [/home/vasu/OpenFOAM/vasu-2.3.1/platforms/linux64GccDPOpt/bin/pasrChem1dToFoam_m] Error 1
这样的东西。