Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Problems getting PHP to run Linux system commands

    Hello all,

    Trying to get PHP script to execute Linux commands...the same commands worked using a shell script on Linux machine. But when I try to execute the same commands in the PHP script I get the following error:

    couldn't get a file descriptor referring to the console

    Here is the PHP code:

    PHP Code:
    <php
    $remote_file 
    file.txt

    system
    ("ftp -nv <<EOF");
    system("open 'ftp.etl.noaa.gov'");
    system("user 'user_name' 'passwd'");
    system("cd et7/data");
    system("mget $remote_file");
    system("y");
    system("EOF");


    ?> 
    Any help is greatly appreciated! Thanks,

    Nick

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    PHP has a builtin ftp library that may be of more use, I wouldn't use system / exec when you don't need to.
    Try using an absolute file path on you're filename, and also be certain to wrap it in quotes.
    BTW, my suspicion is simply that it can't find the file relative to the executing script. I would presume that the system interface takes the cwd out of PHP, which would make it from the executing location. Also, it will inherit the permissions of you're apache use (nobody, apache, whatever), so make certain that apache can read it as well (assuming apache module in use).
    Last edited by Fou-Lu; 10-21-2009 at 04:44 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by nuebelhor View Post
    Hello all,

    Trying to get PHP script to execute Linux commands...the same commands worked using a shell script on Linux machine. But when I try to execute the same commands in the PHP script I get the following error:

    couldn't get a file descriptor referring to the console

    Here is the PHP code:

    PHP Code:
    <php
    $remote_file 
    file.txt

    system
    ("ftp -nv <<EOF");
    system("open 'ftp.etl.noaa.gov'");
    system("user 'user_name' 'passwd'");
    system("cd et7/data");
    system("mget $remote_file");
    system("y");
    system("EOF");


    ?> 
    Any help is greatly appreciated! Thanks,

    Nick
    write a shell script who take as argument that filename and call the script from php.
    From my point of view this is the wrong way to do such things, first because you can do that only with a shell script or if you want only with php. I don't see any reason to mix them.

    best regards

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    PHP has a builtin ftp library that may be of more use, I wouldn't use system / exec when you don't need to.
    Try using an absolute file path on you're filename, and also be certain to wrap it in quotes.
    BTW, my suspicion is simply that it can't find the file relative to the executing script. I would presume that the system interface takes the cwd out of PHP, which would make it from the executing location. Also, it will inherit the permissions of you're apache use (nobody, apache, whatever), so make certain that apache can read it as well (assuming apache module in use).
    I was thinking to something else that break that script:

    PHP Code:
    <php
    $remote_file 
    file.txt

    system
    ("ftp -nv <<EOF"); // <-- open a file descriptor and wait for EOF?
    system("open 'ftp.etl.noaa.gov'"); // <- this try to open another file descriptor, or I'm wrong?
    system("user 'user_name' 'passwd'"); // <- another one?
    system("cd et7/data");
    system("mget $remote_file"); // <- +1
    system("y"); // <- this is a short form for 'yes' probably but, ... +1
    system("EOF"); // <- who get this EOF?, +1 :)

    ?> 
    I guess that after first system, anything is useless, including my comments,

    best regards

  • #5
    New Coder
    Join Date
    Jun 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the comments,

    Do not have access to the computer now...but the file, file.txt, changes every hour, so I already have a script written in PHP to come up with the actual name of file at that specific hour. If there is some way to change the file name in the shell script I could do it that way...but as of now I don't know a way to do it using a shell script. More comments appreciated, thanks,

    Nick

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by nuebelhor View Post
    Thanks for the comments,

    Do not have access to the computer now...but the file, file.txt, changes every hour, so I already have a script written in PHP to come up with the actual name of file at that specific hour. If there is some way to change the file name in the shell script I could do it that way...but as of now I don't know a way to do it using a shell script. More comments appreciated, thanks,

    Nick
    I don't understand do you have or not a shell script who work with a given filename, no matter what name?
    Let's say your shell script is 'myshellscript.sh', then you can call it from php:

    PHP Code:
    $remote_file "file.txt";
    system("./myshellscript.sh $remote_file"); 
    inside the shell script you must have something like that:
    Code:
    txtfile=$1
    and somewere inside you use $txtfile shell variable.
    I guess you don't need to change the filename, maybe I'm wrong, depends of what is in your code.

    a better way, suggested by Fou-Lu, is to use php ftp module:
    http://www.php.net/manual/en/book.ftp.php

    this way you don't need any shell script.
    I don't know how your shell script looks but as far as I know this is a job for expect not bash/sh since you must wait for a ftp connection and provide authentification data.

    best regards

  • Users who have thanked oesxyl for this post:

    nuebelhor (10-22-2009)

  • #7
    New Coder
    Join Date
    Jun 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks I will try that.

  • #8
    New Coder
    Join Date
    Jun 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thanks

    Thanks oesxyl that worked!

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by nuebelhor View Post
    Thanks oesxyl that worked!
    you are welcome. What solution did you choose, with shell script or with all code in php using ftp module?

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •