1

First I tried to pause youtube-dl process and resume it when needed using bash. It did not work out. Now I am trying to suspend and resume processes using python. I have taken guideline from Pausing a process? Now my script looks like:

#! /usr/bin/env python3

import psutil
import time

x = [p.info for p in psutil.process_iter(attrs=['pid', 'name']) if 'youtube-dl' in p.info['name']]
mypid=x[0]['pid']
print(mypid)

p = psutil.Process(mypid)
p.suspend()
time.sleep(10)
p.resume()

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, and replace

x = [p.info for p in psutil.process_iter(attrs=['pid', 'name']) if 'youtube-dl' in p.info['name']] 

with

x = [p.info for p in psutil.process_iter(attrs=['pid', 'name']) if 'tail' in p.info['name']] 

then it pause and resume the tail command as intended. However, it does not work with youtube-dl command as mentioned above.

furas
  • 119,752
  • 10
  • 94
  • 135
Ahmad Ismail
  • 8,816
  • 4
  • 37
  • 60
  • as I know `fg` can use `pid` instead of `%1` and then you could try to use `fg` in python. – furas Jan 28 '21 at 15:38
  • do you ger any error when you try to `resume()` it? – furas Jan 28 '21 at 15:40
  • it does not show anything. Probably the process resume in the background. I see nothing. One thing, if I use `pid` with `fg` then how will it know on which terminal the command will come in foreground? Is there any command like "resume process id 1234 and bring it in the foreground of x terminal"? – Ahmad Ismail Jan 28 '21 at 15:45
  • 1
    I don't know - I used `fg` only manually in terminal. And programs should be already assigned to terminal - when you run `ps aux` then you see column `TTY` which shows on which terminal it works. – furas Jan 28 '21 at 15:56
  • @furas I took your guideline. 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 python script will look like. – Ahmad Ismail Jan 29 '21 at 11:33

0 Answers0