I want to know currently running background process by using unix "jobs" command in python environment.
I tried using
import os os.system('jobs')
but it doesn't work for me.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
jobs is not a command, it's a shell builtin. Together with
bg and other shell builtins, it is used to manage all of the "jobs" that are running as child processes of the shell: list them, stop them, put them in the foreground, etc... As such, it doesn't make sense outside the context of the shell to which the jobs belong.
The equivalent concept in Python would be objects created using the subprocess module. Using that module, you can start, kill, and wait for subprocesses. For example, if you create multiple subprocesses using
subprocess.Popen and store the return values of all of the
subprocess.Popen calls in a Python array, the contents of that array would be the equivalent of the output of
jobs in a shell.
Jobs are an internal notion of the shell. A job is in fact a subprocess that the shell remembers having launched. You can't manipulate a shell's job table from outside that shell.
You can manipulate the processes as such. It's impossible to tell whether a process is a job of a given shell, all you can do is guess. You can list the processes whose parent ID is the shell's PID; you can list processes that are process group leaders and that are running on the same terminal as the shell. See this Stack Overflow question for some ways to obtain information about processes in Python.
If you need to control jobs in your Python script, you should really launch and manage the processes from the Python script, if at all possible. All you need is in the