1

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.

Kusalananda
  • 333,661
Ahmad Ismail
  • 2,678
  • 2
    Just a comment about pgrep youtube-dl + kill -TSTP. This would be much better written as pkill -TSTP youtube-dl. – Kusalananda Jan 27 '21 at 09:43
  • kill -CONT should work (and, of course, pkill -CONT youtube-dl as well). – berndbausch Jan 27 '21 at 09:50
  • 1
    @berndbausch Try it with e.g. top, or any other command. Sending CONT to it will not put it back into the foreground of the interactive shell where it was started. – Kusalananda Jan 27 '21 at 10:16
  • @Kusalananda I tried it with top 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. Since youtube-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:12
  • we can get the terminal name using ptsname=$(ps aux | grep 38648 | awk '{ print $7 }') and continue that process in that terminal using kill -CONT 39905 > /dev${ptsname}. However, I am not still sure how the complete script will look like. – Ahmad Ismail Jan 29 '21 at 11:34
  • 1
    If you prefix your youtube-dl command with setsid 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

0 Answers0