Execute perl in PHP

35,550

Use full path to execute the script.

exec("/usr/bin/perl /full/path/to/Script.pl $username $password",$output);

Regards,

Share:
35,550
Suresh kumar
Author by

Suresh kumar

Software Engineer in the Field of Web Development

Updated on July 09, 2022

Comments

  • Suresh kumar
    Suresh kumar almost 2 years

    Possible Duplicate:
    Calling Perl script from PHP and passing in variables, while also using variablized perl script name

    I want to execute a perl script through PHP. I use exec() to execute the perl script. It works in my machine but does not work on the server. The Server is based on CentOS Linux.

    I gave full permission (777) to both the PHP and the perl script file. When I try to execute, I get the following error in error_log

    sh: /perl: No such file or directory
    

    I tried to execute using the following ways

    exec("perl -w Script.pl $username $password",$output);
    exec("/usr/bin/perl -w Script.pl $username $password",$output);
    exec("/usr/bin/perl Script.pl $username $password",$output);
    

    I also tried by using the system function

    $output = system("perl Script.pl $username $password");
    

    Nothing happening when I try this.

    I also tried to execute perl by using the passthru() function

    passthru("/usr/bin/perl -w Script.pl $username $password",$output);
    

    When I execute this line, $output prints 127 and nothing happens in the script.

    I checked whether file is executable or not with the is_executable() function. It shows that the file is not executable.

    Code as below

    $file = 'Script.pl';
    
    if (is_executable($file))
    {
        echo $file.' is executable';
    }
    else
    {
        echo $file.' is not executable';
    }
    

    If I execute perl through the terminal, it works fine but when I try to execute through PHP, it is not working.

  • Suresh kumar
    Suresh kumar almost 12 years
    Already i try this.. Not working.
  • KingsInnerSoul
    KingsInnerSoul almost 6 years
    PHP executes as the apache user www-data. Your script is probably needs to run as a sudo, but PHP wont allow it. At least for debiab, run visudo, and then add www-data ALL=(ALL:ALL) NOPASSWD: /pat/to/your/script.pl, then change permission so only root can edit the file (safety): sudo chown root:someuser /path/to/your/script.pl, and lastly make it executable sudo chmod u+rwx /path/to/your/script.pl