I am trying to solve the problem given in pause youtube-dl when network is disconnected and resume when it is connected again
To stop and resume process, I have taken guideline from How to suspend and resume processes
The problem is, the following works:
processid=$(pgrep youtube-dl)
kill -TSTP $processid
When I run the above script, The terminal running youtube-dl
shows:
zsh: suspended youtube-dl
% jobs
[1] + suspended youtube-dl
I have to go to the terminal and type the following command to continue the process:
% fg %1
[1] + continued youtube-dl
How to resume processes from a script instead of going to the terminal and typing a command?
Just for the sake of being thorough, If I run tail -f ~/.xsession-errors
, I can pause it from the script using kill -TSTP $processid
and resume it using kill -CONT $processid
. It does not invoke the job control.
pgrep youtube-dl
+kill -TSTP
. This would be much better written aspkill -TSTP youtube-dl
. – Kusalananda Jan 27 '21 at 09:43kill -CONT
should work (and, of course,pkill -CONT youtube-dl
as well). – berndbausch Jan 27 '21 at 09:50top
, or any other command. SendingCONT
to it will not put it back into the foreground of the interactive shell where it was started. – Kusalananda Jan 27 '21 at 10:16top
and confirmed what you said. However, I then tried it with this snippet:( while :; do echo $i; sleep 2; i=$((i+1)); done )
also running in the foreground, and that could be stopped and continued with SIGTSTP and SIGCONT, respectively, although it was continued in the background.top
reads from the terminal, which causes problems when running in the background. Sinceyoutube-dl
is unlikely to require terminal input or running in the foreground, SIGCONT should work in this particular case. – berndbausch Jan 27 '21 at 11:12ptsname=$(ps aux | grep 38648 | awk '{ print $7 }')
and continue that process in that terminal usingkill -CONT 39905 > /dev${ptsname}
. However, I am not still sure how the complete script will look like. – Ahmad Ismail Jan 29 '21 at 11:34setsid
it will run in a new terminal session so you can kill it with-stop
and-cont
without the change in state being seen by the shell. – meuh Jan 29 '21 at 12:51