0

So I've got this to find out if a process is running or not:

os.system("ps aux | grep [my]process")

I use the square brackets so I don't get back the grep command too.

Altho when I try to do something like

'yes' if os.system("ps aux | grep [my]process") else 'no'

I always get no, even if in fact python print the line with the info of the process.

Pretty sure that there must be some sort of misunderstanding from my side...I assume that if the output of os.system is not zero, the expression resolve in true, so I should get 'yes'. But this does not happen at all, I get no, even if the process is there running, and the command return correctly the info about the process.

What am I doing wrong here?

Thanks!

4 Answers4

5

You have the logic the wrong way round. grep is returning 0 when there are some matching lines

using the subprocess module is a better idea anyway. You can get the output of ps aux and examine it in your program. Although parsing the output of ps is always going to be fairly fragile

eg:

import subprocess
'yes' if 'myprocess' in subprocess.check_output(['ps','aux']) else 'no'
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • Thanks, I was not aware that os.system would return exit status (it prints out the command output, so my assumption was that it would return true or false based on that). What would you suggest to check if I have a process running then, if the output of ps aux is fragile? –  Jun 03 '13 at 06:17
  • 1
    @newbiez, Unfortunately it's platform specific. There are some [questions](http://stackoverflow.com/questions/38056/how-do-you-check-in-linux-with-python-if-a-process-is-still-running) on SO already. http://stackoverflow.com/questions/2703640/process-list-on-linux-via-python – John La Rooy Jun 03 '13 at 06:29
  • Strange, I get "attributeError: 'module object has mo attribute 'check_output' ". I did the import subprocess, but when I try to run the string I get this error. Maybe I have an old Python? I am on Mac Lion –  Jun 03 '13 at 09:18
  • Mystery solved: you cannot use check.output on mac, since the new version of Python does not use that anymore. Gotta use subprocess.call –  Jun 07 '13 at 04:07
  • @newbiez, what version of Python are you using? `subprocess.check_output` is certainly not deprecated. Make sure you have the underscore, not a dot as you did in your comment. – John La Rooy Jun 07 '13 at 04:30
  • It is not deprecated, it is just too new for my version of Python (2.6, while it was introduced in 2.7). This is where I've got the news:https://github.com/CellProfiler/CellProfiler/issues/265 –  Jun 07 '13 at 04:35
3

The output of os.system is not what is printed on the screen.

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variable COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.ù

http://docs.python.org/2/library/os.html#os.system

Dek Dekku
  • 1,441
  • 11
  • 28
  • 1
    Much appreciated! so it returns just the exit status code; in fact I've changed the if so it check for 1 and I get 'yes', while if I misspell the process name or check for 0 I get in fact 'no'. –  Jun 03 '13 at 06:13
  • Sorry, I meant the other way around; code 0 means exit without errors, indeed. –  Jun 03 '13 at 08:01
2

os.system returns exit status of the command executed. If the command is successful, it returns 0, or else non-zero number. Example:

>>> t=os.system("ls")
>>> t
0
>>> t=os.system("lsas")
sh: 1: lsas: not found
>>> t
32512
>>> 
rajpy
  • 2,436
  • 5
  • 29
  • 43
1

Use subprocess.call() or subprocess.check_call()

Elazar
  • 20,415
  • 4
  • 46
  • 67