Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

How to suspend and resume processes from a python script

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...