running UNIX with perl

J

joe

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
 
B

Brian McCauley

joe said:
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\\
 
T

Thens

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.
 
J

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.

TIA again!
 
T

Thens

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.
 
M

Matthew Weier O'Phinney

* joe said:
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.
 
T

Thens

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.
 
J

joe

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

Thanks a lot!
 
M

Martien Verbruggen

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top