Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Using a Perl Script To Execute a Linux Command

by kgill (Initiate)
on Jan 23, 2008 at 16:01 UTC ( #663817=perlquestion: print w/replies, xml ) Need Help??
kgill has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am a Unix/Linux server administrator for a company, and I am a "newbie" in the purest sense of the word in terms of Perl. I'm able to make Perl files using the VI in Unix. I'm trying to get two scripts running--one to stop or start my database server that is running Oracle and one to start or stop my application server that is running Oracle. Currently, I'm entering the commands manually, but I would like to schedule the scripts to run every Friday to shutdown and Monday to startup.

A few of the commands needed to start and stop the database and application servers involve changing directories to execute the commands. How can I write a nice, clean script that can do this?

Thanks, Kris

P.S. Here's a mock-up of what I'm trying to accomplish:



login <user>

cd /opt/SIV/inst/apps/SIV_<servername>/admin/scripts ./ pass/pass

login <user>

cd /opt/SIV/db/tech_st/10.2.0 . ./SIV_<servername>.env

sqlplus / as sysdba

shutdown immediate


lsnrctl stop VIS

A similiar script would be needed for the startup.

  • Comment on Using a Perl Script To Execute a Linux Command

Replies are listed 'Best First'.
Re: Using a Perl Script To Execute a Linux Command
by MidLifeXis (Monsignor) on Jan 23, 2008 at 16:39 UTC

    Just because you bought a new hammer does not mean it is the right tool for putting in a screw.

    I would use a cron script, probably as root (or another user using sudo), and call the various stuff as the appropriate user from a shell script.

    To use perl, you would need to handle loading the env files, talking to the sqlplus binary, and other things where there are already examples to do these scripts from a shell.


      Good point. Thank you for your response. I'm looking at doing this with cron right now.

      Thanks again,


Re: Using a Perl Script To Execute a Linux Command
by igelkott (Priest) on Jan 23, 2008 at 16:47 UTC
    What you ask isn't trivial (IMHO). To start, you might look into Expect to send commands to launched programs (like sqlplus). There are other ways to do this but it seems to fit best with what you've outlined here.

    It might be easiest to have most of the startup/shutdown controls in a Perl script local to that machine so your local script just needs to login and start the remote script.

Re: Using a Perl Script To Execute a Linux Command
by Anonymous Monk on Jan 24, 2008 at 08:05 UTC
    Use system() for the commands (or IO::Open2 if they need IO) and chdir() to change directories. For sleeping, you could either ignore the time it takes to execute the commands, use the start/stop times to put in a correction (which will gradually increase the longer your script is on), or a time-$time like construction employing the % operator. Or you could use a cron script, which would probably be a better choice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://663817]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2018-06-24 19:11 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.