11

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.

Community
  • 1
  • 1
Suresh kumar
  • 2,002
  • 1
  • 19
  • 28
  • @K102 Sorry ya... I mistakenly typed in this post.. – Suresh kumar Jun 20 '12 at 06:57
  • @Gordon Thanks for your link.. Still i have the same problem.. – Suresh kumar Jun 20 '12 at 07:14
  • Perhaps you're not allowed to make any system calls from php because of some strict security settings? Check with a simple command like `ls` or `cwd`. If you only execute the perl script from the command line with the perl interpreter up front, you don't have to set the execute permission. `664` should do. Otherwise it might be executable from a browser if the server is set up in that way. – simbabque Jun 20 '12 at 07:25
  • what does the output of `exec('which perl', $output);` give you? – Ben Jun 20 '12 at 07:27
  • @Ben When i exec which perl , following error occur sh: /which: No such file or directory.. – Suresh kumar Jun 20 '12 at 07:50
  • @simbabque I agree with .. when i check the phpinfo, Safe_mode is on in lOcal value and Off in Master value. i want to disable Local value of safemode. some site instruct me to disable Safe mode.. even i change the php.ini, Local value is still on. I think this is the problem.. can u help me to resolve this – Suresh kumar Jun 20 '12 at 07:52
  • Is your website hosted in a "jailed" environment? A jailed environment is one where most of the UNIX commands are disabled for security. Is perl installed (and/or enabled) in the first place? – Salman A Jun 20 '12 at 07:53
  • @SalmanA Perl is installed in my server as well as enabled. when i execute perl script alone, it works fine. – Suresh kumar Jun 20 '12 at 07:54
  • 4
    I solved this problem by Disable Safe_mode in php.ini – Suresh kumar Jun 20 '12 at 08:59
  • Great. :) Post it as an answer and accept it yourself so the question is marked as completed. – simbabque Jun 20 '12 at 09:03

1 Answers1

10

Use full path to execute the script.

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

Regards,

user1126070
  • 5,059
  • 1
  • 16
  • 15
  • Already i try this.. Not working. – Suresh kumar Jun 20 '12 at 07:20
  • 1
    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` – KingsInnerSoul Jun 04 '18 at 03:18