I have this php code:

<?php
define('ORACLE_SID', 'MYDB');
define('ORACLE_HOME', '/usr/lib/oracle/11.2/client');

shell_exec('ORACLE_SID=' . ORACLE_SID);
shell_exec('export ORACLE_SID');
shell_exec('ORACLE_HOME=' . ORACLE_HOME);
shell_exec('export ORACLE_HOME');

putenv("ORACLE_HOME=".ORACLE_HOME);
putenv("ORACLE_SID=".ORACLE_SID);
$path = shell_exec('echo $PATH');
$oracle_home = shell_exec('echo $ORACLE_HOME');
$abs_path = str_replace("\n","",$path. ":" .$oracle_home . "/bin");
putenv("PATH=".$abs_path);

echo exec('sqlplus MYUSER/MYPASS@MYDB @/absolute/route/to/script.sql');


?>

Here I export all the environment variables in order to run a sqlplus command. It works fine when I execute it from line command, but when "cron" executes it, it doesn't run the sqlplus command.

I checked the "exports" part on cron by running "echos" over the variables, and it seems to work fine. I also tried running the sqlplus command adding its absolute path, but it doesn't work either...

The corresponding line in crontab:

*/5 * * * * /usr/bin/php /home/myusr/script.php >> /home/myusr/out.txt

Does anyone know if I'm missing something??

Thanks a lot in advance

有帮助吗?

解决方案

I found a solution: executing from cron but loading the environment of the user who got the right profile in the same instruction.

*/5 * * * * su - myusr -c '/usr/bin/php /home/myusr/script.php'

The .profile of "myusr" must set all the necessary environment variables.

I don't know exactly why it doesn't load the environment variables executing the sentences from the script (I even tried calling the ".profile" from script, and it didn't work either), would be nice if someone could give us some light :)

Thank you

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top