Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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 :-).

When a cron job is run from the users crontab it is executed as that user. It does not however source any files in the users home directory like their .profile, .cshrc, or .bashrc or any other file. If you need cron to source (read) any file that your script will need you should do it from the script cron is calling. Setting paths, sourcing files, setting environment variables, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top