Reputation: 7502
How can I get a list of running jobs in python.
ie. I basically want the output of the jobs
command in a string or list or set.
I am currently trying
p1 = subprocess.Popen(['jobs'], shell=True, stdout=subprocess.PIPE)
n = p1.communicate()[0].strip()
But this doesnt seem to be working
Upvotes: 1
Views: 2026
Reputation: 400314
You have to keep track of it yourself. The shell keeps track of all of its subprocess jobs, but you're not a shell, so you have to do the tracking yourself. jobs
is a Bash shell builtin command, not an actual executable which is run. When you do subprocess.Popen(['jobs'], shell=True)
, you're spawning a new shall and asking it for its jobs, which is of course empty since it's a new shell without any running jobs.
If you can't keep track of your own running jobs, you're going to have a harder time. On Linux, you could parse /proc
and look for all processes which have you as a parent. On Windows, you could do something like this using a wrapper such as pywin32 or the ctypes
module to access the Win32 API.
Upvotes: 6