I want to know currently running background process by using unix "jobs" command in python environment.

I tried using

import os

but it doesn't work for me.


jobs is not a command, it's a shell builtin. Together with fg, 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.

| improve this answer | |
  • Can you please give some example how to do it.. – Manas Sahu Mar 18 '15 at 6:56
  • What do you want to do exactly? There are plenty of examples in the subprocess module documentation (linked above). – Celada Mar 18 '15 at 7:41
  • I have python script that execute set of shell script in background according to user requirement. I want to know the status of the background process that the user executed i.e running or Done. – Manas Sahu Mar 18 '15 at 8:27
  • Have a look at Popen.poll. It's documented as "Check if child process has terminated." which is just what you are looking for. If it returns None then the subprocess has not yet terminated. – Celada Mar 18 '15 at 8:44

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 subprocess module.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.