W
W
waltaki2020-08-13 00:36:27
linux
waltaki, 2020-08-13 00:36:27

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

3 answer(s)
V
Vamp, 2020-08-14
@waltaki

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

S
Saboteur, 2020-08-13
@saboteur_kiev

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.

sh doesn't eat anything and it can only automatically pass sighup to child processes, which it won't pass because you have an interactive session and signal 9 doesn't reach the process - the process is killed by the kernel.
Actually, until it is clear why you run sh from which you run jar, it is not very clear what is happening.
Perhaps you need to do it like this:
java -jar  ol.jar &
echo $! > ol.pid

and therefore get the pid of the java process in the file, then work with it.

A
Alexey Cheremisin, 2020-08-13
@leahch

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 question

Ask a Question

731 491 924 answers to any question