Answer the question
In order to leave comments, you need to log in
How to send a signal to a child sh process?
There is a command , the PID of the bash process is known to me, but its child is not. I send signals to the PID of the sh process, and only the 9th signal works (logically, it kills it), sh eats the rest of the signals itself and does not send it to the child process.
How to make sh forward signals to child process? Maybe there is an argument.
pstree works, but still looks like a crutch. sh -c "java -jar ol.jar"
Answer the question
In order to leave comments, you need to log in
It is possible to replace the sh process with a java process using the exec command:
In this case, the child process will take the place of the parent. In particular, it will get the PID of a bash process that you already know. Of course, if there are any other commands in the script after calling java, then they will never be executed, since the bash process ceases to exist at the moment the exec is called.
If you still need to execute some final commands at the end of the java process, then it is possible to hang your own signal handlers:
sh -c "exec java -jar ol.jar"
#!/bin/sh
# Устанавливаем обработчик сигналов HUP INT QUIT TERM и псевдосигнала EXIT.
# В обработчике отправляем сигнал TERM процессу, чей pid будет записан в
# переменной CHILDPID.
trap 'kill $CHILDPID' HUP INT QUIT TERM EXIT
# Запускаем java в фоне.
java -jar ol.jar &
# Сохраняем PID только что запущенного дочернего java процесса.
CHILDPID=$!
# Следующая команда ставит процесс sh на паузу.
# Пауза снимется после завершения фонового java процесса.
# Обработчик сигналов при этом остаётся работоспособным и будет
# корректно отрабатывать во время паузы.
wait
# В этом месте java уже завершилась и можно что-нибудь сделать.
# Например, подчистить временные файлы.
echo Cleanup actions
sh -c "java -jar ol.jar"
Why such a command, if you can immediately java -jar ol.jar ?
I send signals to the PID of the sh process, and only the 9th signal works (logically, it kills it), sh eats the rest of the signals itself and does not send it to the child process.
java -jar ol.jar &
echo $! > ol.pid
And write a launcher that will get pid from a variable $!
? https://unix.stackexchange.com/questions/30370/how...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question