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.
question from:
https://stackoverflow.com/questions/65936832/how-to-suspend-and-resume-processes-from-a-python-script 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…