Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

OpenOffice Quickstarter (Linux) in Perl

by mpolo (Chaplain)
on Mar 08, 2004 at 11:20 UTC ( #334759=perlquestion: print w/replies, xml ) Need Help??

mpolo has asked for the wisdom of the Perl Monks concerning the following question:

I've finally gotten sick and tired of OpenOffice.org taking over 30 seconds to open on my computer, when M$Word running uner Wine (Crossover) is near instantaneous.

Unfortunately, the Gnome OOOQuickstart project is still supporting only Gnome 2.2, so I decided to write a stupid, brute force solution (sort of) in Perl.

Here is my (basically working) code:

#!/usr/bin/perl use strict; while (1) { my $running=`ps ax|grep "soffice.bin -quickstart"`; if ($running !~ /openoffice/) { # don't match if only the "grep" is there sleep 5; # give the process time to end to avoid silly crashes... system("/usr/lib/openoffice/program/soffice.bin -quickstart -plugi +n"); } }

My question (given the category I posted under, you knew it was coming to this...) is what would be a "good" Perl solution to this? I mean, this is functional and all, but I'm just making repeated system calls -- my program doesn't deserve the name of perlscript, because it's really just a shellscript in Perl clothing.

Replies are listed 'Best First'.
Re: OpenOffice Quickstarter (Linux) in Perl
by halley (Prior) on Mar 08, 2004 at 14:01 UTC
    Is there some reason that perl's built-in grep is less useful than starting a whole 'nother piped process?
    while (1) { if (not grep { /soffice\.bin -quickstart/ } `ps ax`) { sleep 5; system('/usr/lib/openoffice/program/soffice.bin -quickstart -plugi +n'); } }
    Now you don't have to worry about the whole "grep in ps" work-around.

    --
    [ e d @ h a l l e y . c c ]

Re: OpenOffice Quickstarter (Linux) in Perl
by Abigail-II (Bishop) on Mar 08, 2004 at 11:40 UTC
    Perl being a glue language, this is a good Perl solution.
    I mean, this is functional and all,
    Right. What else do you need? It's functional, it's less than 10 lines of code. Be happy.

    Abigail

Re: OpenOffice Quickstarter (Linux) in Perl
by Corion (Pope) on Mar 08, 2004 at 11:49 UTC

    To prevent the ps ax|grep combo from finding itself, I encode one char within a character class like this:

    ps ax|grep '[s]office.bin -quickstart'

    I would maybe move the whole loop out of the program and put it into a cron job that runs every minute instead, but other than that, I'm not exactly sure what you're looking for.

      The "ps not finding itself" problem is usually soluable with pgrep, which is present in Solaris, GNU/Linux, and probably most other modern OSes of similar type. Functionally, it's not really any different than your trick above, but pgrep is so useful I thought it was worth a separate mention.

      I would think a better way - rather than looping based on timeout - would be to fork, exec() OOo in the child, and then join the child. That would have the loop effect, but without the constant polling. It would (probably) remove the need to look for the process too, which could be useful (do you know whether or not your user has the ability to talk to the other process, for example? I don't know the answer to that, but I would think it would be an obvious question).

      Using the c option of GNU grep uses only the executable name

      --
      I'm not belgian but I play one on TV.

Re: OpenOffice Quickstarter (Linux) in Perl
by zakzebrowski (Curate) on Mar 08, 2004 at 12:32 UTC
    I agree with the laziness idiom. However, you may want to look at the Proc::Process module. It allows you to look at a process table on unix / linux, so you don't have to shell out to do the ps -aef | grep ...


    ----
    Zak - the office
Re: OpenOffice Quickstarter (Linux) in Perl
by mpolo (Chaplain) on Aug 12, 2005 at 15:35 UTC

    Update for OpenOffice 2.0 (Beta), incorporating some of the suggestions from the thread. The "-quickstart" flag is no longer there, but using the two flags "-nodefault -nologo", you can get the same results. Hopefully this is useful to someone.

    #!/usr/bin/perl use strict; while (1) { if (not grep {/soffice\.bin/} `ps ax`) { sleep 5; # give the process time to end... system("/usr/lib/openoffice.org2.0/program/soffice.bin -nodefault +-nologo"); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://334759]
Approved by EvdB
Front-paged by htoug
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2020-08-10 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (57 votes). Check out past polls.

    Notices?