4

I've got a PHP script that I want to run through cron. It should load different configuration information depending on if the script is running on my local server, the QA server, or the production server. The problem is that when the script is run through cron, it doesn't have any of the identifying $_SERVER information that I use to figure out which environment the script is running on.

The only thing I could think of was to have the cron job itself attach a parameter to the script, but I was hoping there was a better way where I wouldn't have to do that. Is there any other way to identify which server the script resides on when running it from cron?

Thanks.

2
  • How are the servers different? Check on IP-address, check for existence of a path, contents of a file...
    – Konerak
    Jan 12, 2011 at 19:44
  • Can't use gethostname(), because I'm on PHP 5.2. And I'm not sure how to get the IP address without the $_SERVER variables. Otherwise, the servers are set up with the same code, so I could do something like check for a path, but I'd have to insert it manually. Jan 12, 2011 at 19:48

2 Answers 2

3

I solve the problem the next way: pass to script environment variables.

In case of .htaccess / httpd.conf: SetEnv SITE_NAME x1

In case of cron (run a script):

#!/bin/bash
export SITE_NAME=x1;
php /path

In both cases PHP-code to get this environment variable is: getenv('SITE_NAME');

x1 — is an example site name.

3
  • That seems like a great solution, but how permanent are those environment variables? If the box is reset, do they disappear? Jan 12, 2011 at 20:00
  • The variables in .htaccess files or server configs (like httpd.conf) is absolutely permanent. In bash script the variable is set every runtime before PHP script is executed. You just have to run shell-script from cron (not a direct php call). So the solution is "permanent" and works for me properly for a long time. Jan 12, 2011 at 20:03
  • Oh wait, I see now. The script I run would add the variable right before it runs the script. That's perfect. Thanks! Jan 12, 2011 at 20:03
0

getenv('COMPUTERNAME') will return the computer name it is running on...

However, if your script does different things in production to test, how do you know that it is correct in production?

2
  • That function doesn't return anything when I try it. And the script doesn't do anything different, it just has different configuration variables. For example, the database connection information is different. Jan 12, 2011 at 20:11
  • Requested solution can be used to select proper configuration file (ex. database connection settings). It not seems to be bad practice. I use this techniques to detect site name, because I have different sites running on one copy of code (using virtual hosts on dedicated server). Jan 12, 2011 at 20:12

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.