Main index > Python programming > python commands module

By cs-cam (Desklet Author), on Sun Sep 18 01:20:09 2005: python commands module.

I want to use commands.getoutput() in a desklet I'm working on (almost completed, except for this ;)). I want to get the pid of a process and I was going to just grab the output of pidof, but if there is a better, more pythonic way then I'd be happy to hear it! However I seem to be getting errors in all desklets if I simple insert a call to commands.getoutput() anywhere. I can use that function in other python scripts no problems so I'm guessing that maybe it's conflicting with the adesklets.commands module?

Any help in this regard would be appreciated :)

By syfou (Core Developer & Desklet Author), on Sun Sep 18 02:17:31 2005.

You are right, there are some interferences between commands.getoutput and the adesklets package. You can achieve the exact same result using other calls such as:

Code:


os.popen('insert command here 2>&1').read()


But do you really need to escape to the shell? To my knowledge, there is not consistent and portable way from the core python library to have easy access to the logical equivalent of pidof (you could still use pystatgrab though), but keep in mind pidof is not portable across posix either, and not even available in the default path of non-superusers on many linux systems (gentoo or debian for instance). Better stick to very basic forms such as:

Code:


ps -A | sed -n '/program_name/{s/^[ \t]*\([0-9]\+\).*/\1/;p}'


In place of : "pidof program_name". Would you describe what is the context requiring you to find on-the-fly pids like that? Just curious. :-)

By cs-cam (Desklet Author), on Sun Sep 18 07:43:45 2005.

I am writing a Folding@Home desklet. I couldn't think of a cool way to represent the data so I'm simply cloning the SuperKaramba folding theme. So far it's complete but that shows the pid, I'm assuming of the parent process. I could use statgrab but then it's an extra dependency for something simple like that, it hardly seems worth it...

That is the reason I wanted to use something unix-friendly. I wans't aware that pidof was generally a root-only thing, it isn't in Arch Linux which I use :oops: I'll give your solution a shot and see how it goes, thanks for the advice :D

EDIT:
Okay, it works fine when I paste the command straight into a terminal but not in my script and I don't know why. Basic commands like whoami work fine, I can't see where I messed up.

Code:


def _shell(cmd):
        try:
                output = os.popen('%s 2>/dev/null' % cmd).read()
        except:
                _dPrint('Error reading data from command line')
        
        return output

print _shell("ps -A | sed -n '/FAH502\-Linux\.ex/{s/^[ \t]*\([0-9]\+\).*/\1/;p}'")


That prints nothing but blank line, but at the terminal it displays the pid as it should :(

By syfou (Core Developer & Desklet Author), on Sun Sep 18 14:04:46 2005.

cs-cam wrote:


Okay, it works fine when I paste the command straight into a terminal but not in my script and I don't know why. Basic commands like whoami work fine, I can't see where I messed up.
That prints nothing but blank line, but at the terminal it displays the pid as it should :(

Classic shell programming trap. :wink: Backslashes have a special meaning inside python string literals. Just print out from Python the previous command string in the console to convince yourself. You need to "escape" the string somehow. The most simple way is probably to duplicate every occurrence of the '\' character in the command string.

By darkliquid (Desklet Author), on Sun Sep 18 14:32:08 2005.

Alternately, instead of using sed, you can just read in the output of ps -A and then operate on the string within python.

By syfou (Core Developer & Desklet Author), on Sun Sep 18 15:18:22 2005, last edited on Sun Sep 18 16:22:24 2005.

See the brain damage of too much shell scripting? :headbash: Your are absolutely right, darkliquid.

Code:


[line.strip().split()[0] for line in os.popen('ps -A').readlines() if 'FAH502-Linux.exe' in line]

By cs-cam (Desklet Author), on Sun Sep 18 16:19:07 2005.

syfou wrote:

cs-cam wrote:


Okay, it works fine when I paste the command straight into a terminal but not in my script and I don't know why. Basic commands like whoami work fine, I can't see where I messed up.
That prints nothing but blank line, but at the terminal it displays the pid as it should :(

Classic shell programming trap. :wink: Backslashes have a special meaning inside python string literals. Just print out from Python the previous command string in the console to convince yourself. You need to "escape" the string somehow. The most simple way is probably to duplicate every occurrence of the '\' character in the command string.

:roll: Doh! I completely forgot to escape those characters. I thought to do the hyphen and period in the program name but in my hurry I didn't even think of the others. Thanks, I can fix it this afternoon after a day at the go kart track :D


adesklets is proud to be hosted on:

SourceForge.net Logo

Back to adesklets.sf.net.