tags:

views:

598

answers:

3

I am running the following script on a cronjob...

cd /etc/parselog/
php run_all.php >/dev/null

and am getting the following errors:

[05-May-2009 20:30:12] PHP Warning:  PHP Startup: Unable to load dynamic library './pdo.so' - ./pdo.so: cannot open shared object file: No such file or directory in Unknown on line 0
[05-May-2009 20:30:12] PHP Warning:  PHP Startup: Unable to load dynamic library './mysql.so' - ./mysql.so: cannot open shared object file: No such file or directory in Unknown on line 0
[05-May-2009 20:30:12] PHP Warning:  PHP Startup: Unable to load dynamic library './mysql.so' - ./mysql.so: cannot open shared object file: No such file or directory in Unknown on line 0
[05-May-2009 20:30:12] PHP Warning:  PHP Startup: Unable to load dynamic library './mysqli.so' - ./mysqli.so: cannot open shared object file: No such file or directory in Unknown on line 0
[05-May-2009 20:30:12] PHP Warning:  PHP Startup: Unable to load dynamic library './odbc.so' - ./odbc.so: cannot open shared object file: No such file or directory in Unknown on line 0
[05-May-2009 20:30:12] PHP Warning:  PHP Startup: Unable to load dynamic library './pdo.so' - ./pdo.so: cannot open shared object file: No such file or directory in Unknown on line 0
[05-May-2009 20:30:12] PHP Warning:  PHP Startup: Unable to load dynamic library './pdo_mysql.so' - ./pdo_mysql.so: cannot open shared object file: No such file or directory in Unknown on line 0
[05-May-2009 20:30:12] PHP Warning:  PHP Startup: Unable to load dynamic library './pdo_odbc.so' - ./pdo_odbc.so: cannot open shared object file: No such file or directory in Unknown on line 0
[05-May-2009 20:30:12] PHP Warning:  PHP Startup: Unable to load dynamic library './pdo_sqlite.so' - ./pdo_sqlite.so: cannot open shared object file: No such file or directory in Unknown on line 0
[05-May-2009 20:30:12] PHP Fatal error:  Call to undefined function mysql_connect() in /etc/parselog/stats_downloads.php on line 5

However, when I run the same script from the command line, logged in- it works perfectly without error.

This is my $PATH string at the prompt:

$PATH = /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin

Any ideas or suggestions? I am certain when running the cronjob, it does not have a lib path or something included. I even tried adding the exact path

+3  A: 

Tip 1: Ensure you have exactly the same $PATH in the cron job:

cd /etc/parselog/
export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
php run_all.php >/dev/null

Tip 2: Make sure all other environment variables match as well. Dump the environment with the command env, and add the corresponding exports to your cron job.

pts
Well, I got the export path in, I think I had a $ in front of the PATH in the export. My env is:HOSTNAME=*****TERM=xtermSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=70.48.253.46 61056 22OLDPWD=/etcSSH_TTY=/dev/pts/0USER=rootMAIL=/var/spool/mail/rootPATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/binINPUTRC=/etc/inputrcPWD=/etc/cron.hourlySHLVL=1HOME=/rootLOGNAME=rootLESSOPEN=|/usr/bin/lesspipe.sh %sG_BROKEN_FILENAMES=1_=/bin/env
Mike Curry
So it looks as if there is no important env vars I am missing.
Mike Curry
Could you please modify the question so it includesthe most up-to-date cron job?
pts
+2  A: 

Generally when something fails during cron but runs on the command line it's permissions, working directory, or environment variables.

It looks like the php executable itself isn't loading the libraries, the problem might not be in your script.

Tom Ritter
A: 

When you run a script in a cron job, it is run in a limited shell with very few environment variables set. From your errors, it looks like it may be the LD_LIBRARY_PATH that needs to be set as the errors pertain to a bunch of shared libraries, but that is just a guess. It looks like the proper library paths for PHP are not being set up correctly in the cron script.

You can capture the environment used when running the cron script by adding the env command to the script and capturing the output. Compare this to the env output at the prompt and look for variations. Most likely they will be from commands issued in either your local or system profile or bashrc (or maybe even .login) files that set up library paths. I'd specifically look for paths and variables related to PHP since that seems to be your issue.

I had to deal with this issue several times on my last project and the basic solution is to:

  1. determine the minimum environment needed to execute the script
  2. create a short script that sets up the desired environment
  3. add a call to the script from item 2 in the script launching your applications so that the environment is set up properly.
  4. Test thoroughly to make sure you didn't miss anything :-).
dagorym