[Tutor] *nix-specific Python Scripting [Off Topic]
ZIYAD A. M. AL-BATLY
zamb at saudi.net.sa
Thu May 12 23:18:29 CEST 2005
On Thu, 2005-05-12 at 13:37 -0400, William O'Higgins wrote:
> I am trying to learn Python by translating some of my Perl scripts. One
> thing that is eluding me is assigning the results of system calls to
> variables. Here's what I want to do in Perl:
>
> $isxrunning = `ps -C startx | grep "startx"`;
>
> if ($isxrunning =~ "startx") {
> do something;
> } else {
> do something else;
> }
>
> It is a simple check to see if the X server is running (this is
> inelegant, but it works - if you have a better way, I'd love to know
> about it, but I would like to be able to do things like this in Python -
> so I might better write scripts with it).
> ...
(This is off topic! Sorry.)
Why using “ps -C startx | grep "startx"”? Why not “ps h -C startx”[1]?
Even better, why not “pidof startx”?
As for another way to check for X, did you try checking for the lock
file? (Usually in “/tmp/.X0-lock”) Combined with checking the $DISPLAY
environment variable, this is a much better and elegant solution in my
not-so-experienced opinion.
Notes:
1. This probably wont work with FreeBSD (and all other *BSD(s) in
that matter), instead, try the “--no-headers” option. See the
manual page for “ps”.
Ziyad.
More information about the Tutor
mailing list