running UNIX with perl

Discussion in 'Perl Misc' started by joe, Jun 27, 2003.

  1. joe

    joe Guest

    Dear Gurus,

    I got stuck at the following. I've tried running a UNIX command in my Perl
    CGI-script, but it doesn't work the way I expected it to. When I run the
    next line:

    $ls = `ls > file.txt`; or
    system("ls > file.txt");

    everything is ok. So running a UNIX command is not a problem an sich. But
    when I run something like:

    $ls = `man perl > file.txt`; or
    system("mysqldump --user=username --password='password' dbname tablename >
    file.txt");

    an empty text-file is the result. How come? I'm a verry happy man already
    knowing someone can help me out.

    TIA

    Joe
     
    joe, Jun 27, 2003
    #1
    1. Advertising

  2. "joe" <> writes:

    > I got stuck at the following. I've tried running a UNIX command in my Perl
    > CGI-script, but it doesn't work the way I expected it to. When I run the

    ^^^^^^^^
    > next line:
    >
    > $ls = `ls > file.txt`; or
    > system("ls > file.txt");
    >
    > everything is ok. So running a UNIX command is not a problem an sich. But
    > when I run something like:
    >
    > $ls = `man perl > file.txt`; or
    > system("mysqldump --user=username --password='password' dbname tablename >
    > file.txt");
    >
    > an empty text-file is the result. How come? I'm a verry happy man already
    > knowing someone can help me out.


    Have you tried these things from a command line script? Perlhaps you
    are getting problems because of the CGI environment.

    Are these failing commands emitting anything on STDOUT? (Would you know if
    they were?).

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Jun 27, 2003
    #2
    1. Advertising

  3. joe

    Thens Guest

    On Fri, 27 Jun 2003 17:16:06 GMT
    "joe" <> wrote:

    >Dear Gurus,


    >everything is ok. So running a UNIX command is not a problem an sich. But
    >when I run something like:


    Yes. The problem is not with running unix commands.

    >$ls = `man perl > file.txt`; or
    >system("mysqldump --user=username --password='password' dbname tablename >
    >file.txt");


    >an empty text-file is the result. How come? I'm a verry happy man already
    >knowing someone can help me out.


    CGI programs run in the server as user nobody.

    * Never use relative paths for accessing files
    * Use absolute paths for system executables.

    You might try

    system("/usr/local/bin/mysqldump --user=username --password='password' dbname tablename > file.txt")

    or wherever the installation is. And also how are you accessing the file 'file.txt'. Be sure to access it from the right place.

    Regards,
    Thens.
     
    Thens, Jun 27, 2003
    #3
  4. joe

    joe Guest

    Thanks for the quick response, but i'm nu further. I made a shell script
    with the following lines:

    echo test1 > /opt/guide/www.secretaressebank.nl/HTML/dev/dbdump.txt
    mysqldump --user=username --password='password' dbname tablename >> file.txt
    echo test2 >> /opt/guide/www.secretaressebank.nl/HTML/dev/dbdump.txt

    When i exec this from the bash, it works just fine. But when i run it
    through a CGI-script, it 'forgets' to execute the second and most important
    line. Does it have anything to do with the rights? The CGI-script's rights
    are 755, the shell script's are 777.

    TIA again!
     
    joe, Jun 27, 2003
    #4
  5. joe

    Thens Guest

    On Fri, 27 Jun 2003 18:00:19 GMT
    "joe" <> wrote:

    >Thanks for the quick response, but i'm nu further. I made a shell script
    >with the following lines:
    >
    >echo test1 > /opt/guide/www.secretaressebank.nl/HTML/dev/dbdump.txt
    >mysqldump --user=username --password='password' dbname tablename >> file.txt
    >echo test2 >> /opt/guide/www.secretaressebank.nl/HTML/dev/dbdump.txt
    >
    >When i exec this from the bash, it works just fine. But when i run it
    >through a CGI-script, it 'forgets' to execute the second and most important
    >line. Does it have anything to do with the rights? The CGI-script's rights
    >are 755, the shell script's are 777.
     
    Thens, Jun 27, 2003
    #5
  6. * joe <>:
    > Thanks for the quick response, but i'm nu further. I made a shell script
    > with the following lines:
    >
    > echo test1 > /opt/guide/www.secretaressebank.nl/HTML/dev/dbdump.txt
    > mysqldump --user=username --password='password' dbname tablename >> file.txt
    > echo test2 >> /opt/guide/www.secretaressebank.nl/HTML/dev/dbdump.txt
    >
    > When i exec this from the bash, it works just fine. But when i run it
    > through a CGI-script, it 'forgets' to execute the second and most important
    > line. Does it have anything to do with the rights? The CGI-script's rights
    > are 755, the shell script's are 777.


    It looks like the problem is that the CGI environment path is different
    than your bash path -- which means it can't find where the mysqldump
    executable is. In bash, type 'which mysqldump', and then copy the full
    path it gives you to both your shell script and perl script -- I think
    you'll find it works now.

    The next thing to do if it doesn't work is to find out whether or not
    the mysqldump executable has permissions that allow the CGI environment
    to execute it. If it's permissions allow execution by the CGI
    environment's user, group, or world permissions, than it should work; if
    not, well, you're pretty much hosed.

    --
    Matthew Weier O'Phinney
    http://weierophinney.net/matthew/
     
    Matthew Weier O'Phinney, Jun 27, 2003
    #6
  7. joe

    Thens Guest

    On Fri, 27 Jun 2003 18:00:19 GMT
    "joe" <> wrote:

    >Thanks for the quick response, but i'm nu further. I made a shell script
    >with the following lines:
    >
    >echo test1 > /opt/guide/www.secretaressebank.nl/HTML/dev/dbdump.txt
    >mysqldump --user=username --password='password' dbname tablename >> file.txt
    >echo test2 >> /opt/guide/www.secretaressebank.nl/HTML/dev/dbdump.txt


    Monitor the STDERR, in CGI the servers error log for the details.

    >When i exec this from the bash, it works just fine. But when i run it
    >through a CGI-script, it 'forgets' to execute the second and most important
    >line. Does it have anything to do with the rights? The CGI-script's rights
    >are 755, the shell script's are 777.


    Ignore the other reply.

    When you execute it from a bash your environment is set to access
    mysqldump from your PATH settings. But in a CGI environment your PATH
    settings are not known and hence you need to find out the actual
    location of mysql in your system.

    ~home> which mysqldump

    and invoke mysqldump with the absolute path in the shell script and the perl
    script


    Regards,
    Thens.
     
    Thens, Jun 27, 2003
    #7
  8. joe

    joe Guest

    Great, using the exact address, captured by 'which mysqldump', was the
    solution indeed!

    Thanks a lot!
     
    joe, Jun 27, 2003
    #8
  9. On Fri, 27 Jun 2003 21:06:52 GMT,
    joe <> wrote:
    > Great, using the exact address, captured by 'which mysqldump', was the
    > solution indeed!


    Great.

    Now, to avoid all the mistakes that everyone has already made once
    before while working in a CGI environment, please read the first
    question in the Perl FAQ, section 9, and follow some of the links
    suggested there.

    Martien
    --
    |
    Martien Verbruggen | Unix is user friendly. It's just selective
    | about its friends.
    |
     
    Martien Verbruggen, Jun 28, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,863
  2. Replies:
    12
    Views:
    1,702
    Dave Thompson
    Jan 10, 2005
  3. Replies:
    18
    Views:
    656
    Dave Thompson
    Jan 10, 2005
  4. joe
    Replies:
    8
    Views:
    216
    Matthias Weckman
    Nov 14, 2003
  5. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    312
    Michele Dondi
    Jan 22, 2004
Loading...

Share This Page