java code
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder pb=new ProcessBuilder("python","test.py");
File log = new File("log");
pb.redirectErrorStream(true);
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(log));
Process p=pb.start();
Thread.sleep(100);
p.destroy();
}
python code
import os
import signal
receive_times = 0
def handler(signalnum, frame):
global receive_times
print(u"received", signalnum, frame, receive_times)
receive_times += 1
if receive_times > 0:
exit(0)
def main():
signal.signal(signal.SIGTERM, handler)
while True:
print("pid:", os.getpid())
if __name__ == '__main__':
main()
result:
('pid:', 16622)
('pid:', 16622)
('pid:', 16622)
('pid:', 16622)
(u'received', 15, <frame object at 0x7f49dd25a3d0>, 1)
shell
#!/bin/bash
_term(){
echo "Caught SIGTERM signal!"
kill -TERM "$child" 2>/dev/null
}
trap _term SIGTERM
echo "Doing some initial work...";
docker run --rm --pid=host test1 &
child=$!
echo "$child"
wait "$child"
echo "Doing some initial work 2 ..."
docker run --rm --pid=host test2 &
child=$!
echo "$child"
wait "$child"