Cpanel Cron Job Php Global Variable - php

The cron job process works, but it doesn't read global variables like $ _SERVER in php.
Cron Job Code:
/usr/local/bin/ea-php72 -q /home/userName/public_html/folderName/folderName2/phpFile.php
PHP Code:
print_r($_SERVER['DOCUMENT_ROOT']);
How do we get it to read these global variables?

For document_root it's normal. You run PHP in command line, so you not used a webserver so you don't have a document_root.
So PHP can't give you this information. Other entries of $_SERVER was not gived when run PHP in command line.

There is no server, so $_SERVER is not set.
You are running the script directly as cron cron (as opposed to from a web server accessed by an HTTP request triggered by a cronjob ), then of course it doesn't work.

Related

Openshift cron doesn't run php file

I have 2 .php files in my application - book.php and weather.php. I create a file named "runscript" in /.openshift/cron/minutely. This file contents:
#!/bin/bash
php -f $OPENSHIFT_REPO_DIR/weather.php
This script send me message to phone every minute, it's OK.
Then I replace to:
php -f $OPENSHIFT_REPO_DIR/book.php
This script MUST send me message too, but nothing is happing. But if I just run this script by my webbrowser (go to the http://xxx-xxxxxxx.rhcloud.com/book.php) so I got my message. How is it possible? Magic?
Did you miss the #!/bin/bash part? That's needed to run the shell script.
For why your cron job is not executing, check the cron logs on OpenShift. You can find them at ~/app-root/logs/cron_*.log when you SSH into your gear.
Make sure your cron job is execuable with chmod, and has the shebang line as #gnaanaa says. Also check if you have one of the .openshift/cron/minutely/jobs.{allow,deny} files as they may cause cron to skip your job. (See the cron README for more information.)
And after your cron job is working, you can get rid of the wrapper script runscript and have cron call book.php directly. To do so, place book.php directly into .openshift/cron/minutely, make it executable, and add this shebang to it:
#!/usr/bin/env php
Hope this helps.
I use openshift aswell and executed a php file with a cron aswell.
#!/bin/bash
php ${OPENSHIFT_REPO_DIR}index.php
This executes the script normally at first sight. However no output was produced. The problem was, that all the required php files couldnt be loaded because the working directory was not the same as it would be when loaded by the webserver. Setting the working directoy in the php script itself will prevent this error and makes the script perfectly executable by the cron.
This should help some people to get their script running.

Crontab on AWS EC2 and $_SERVER PHP variables

I have some PHP scripts for database maintenance in my server that requires its periodically execution. Obviously the easiest solution is to schedule its running with system cron.
The scripts require some server variables accessed from $_SERVER, like database hostname, cron parameters, etc.
I can run the scheduled cron commands from command line without any problem, and everything seems to be working fine (calling something like php filename.php). However, when the same commands are executed from cron, the scripts fails and the error reported is like the following:
PHP Notice: Undefined index: RDS_DATABASE in
/var/app/current/app/xx/Db/ConnectionFactory.php on line 8 PHP
Seems that the $_SERVER variable is not correctly initialized when running from cron, but it works from command line. I have tried with crontab -u ec2-user -e but without luck.
I do not want to use wget to run the script as it adds some overhead, and the scripts are hidden from being accessed from HTTP.
Any hint about successfully accessing $_SERVER from command line, but failing when running from crontab?
Had the same issue. Found a solution:
echo "InstanceID: ".get_cfg_var('INSTANCE_ID')."\n";
For some reason it was working fine on my ec2 user but not as a root cron job. Using the function instead of accessing the $_SERVER array solved my problem.
$_SERVER only works if you will run PHP using any web server. If you will use crontab and execute PHP via command line it will not work. You may refer to PHP documentation http://php.net/manual/en/reserved.variables.server.php#refsect1-reserved.variables.server-indices
As #Baminc and #Ankur says the solution is to use get_cfg_var function to get the information because $_SERVER only works when you access it from a web browser.
What I do is the following for example with SERVER_NAME :
if (isset($_SERVER['SERVER_NAME'])) {
$myServerName = $_SERVER['SERVER_NAME'];
} else {
$myServerName = get_cfg_var('SERVER_NAME');
}
Hope this helps!

Run a php script only from Cron or check if request from cron?

How can I run a php script only from cron or is it possible to check if the request comes from cron?
EDİT
I am trying to call php file with a parameter. But none of them work:
php -q /home/domain/public_html/cronjob.php?i=a
php -q /home/domain/public_html/cronjob.php par1
php -q /home/domain/public_html/cronjob.php par1=a
php -q /home/domain/public_html/cronjob.php par1='a'
I can't use $_GET,$_SESSION or $argv. How I call the file with a parameter?
If you're asking this because you need to check if the script is not called from a web request, you can use the php_sapi_name function to see the method of invocation - if it's a cron job, the function should return cli that indicates a command-line call. It does not, however, help distinguish between a cron job and a regular command-line call.
If you also need to distinguish between a cron job and a command-line invocation of the script, you can check the user running the script and seeing if it's the same that should be running the cron job. That could be done, for example, on a *nix system, with posix_getuid or posix_getlogin function.
Lastly, if you're worried someone's logging in as the cron job owner and running the script from command line, you should use a separate user with no shell for the cron jobs.
You could just pass a parameter to your script that says it was called in a cron job.
Take a look at the question: cron-jobs-calling-a-php-script-with-variables
How about passing some auth key to your script which is only stored in the cron_script and the receiver script. On call check if the authkey is transmitted via $_GET['auth_key'] in your receiver script.
Like: http://www.example.com/receiver_script.php?auth_key="WhatEverYouWant"
Allright, I found the solution. If you want to call the php file with a GET parameter you should call file this way:
php -q /home/domain/public_html/cronjob.php name=value
and you can get this data in php file using $_GET:
$_GET['name']
or argv:
$argv[1]
If you want to pass a parameter to just argb :
php -q /home/domain/public_html/cronjob.php value
Other examples that I found in the websites don't work but these work great..

How does PHP act when executed by a shell script?

There is a PHP script I would like to execute by a cron-job but it has to be executed every seconds or every 2 seconds.
(PHP file updates the cover photo of a page via opengraph)
So I decided to write a shell script which is below. But how does php act when executed by a shell script, is it works normally like requested from a browser or what happens ?
Does sessions works ?
#!/bin/bash
while true; do
/path/to/file.php
sleep 1
done;
echo "Stopped" | mail -s "Cron script has stopped." mymail#domain.com
The difference is that you won't have $_GET, $_POST and other http specific stuff available. Sessions also won't work (why would you need them?). You obviously won't be able to set cookies, headers and other such things.
Other than that you can pretty much ignore the fact that you are "in a shell".
See here for more details: http://www.php.net/manual/en/features.commandline.differences.php
Take all the browser aspects away from PHP and that's how it'll act. You get no sessions (unless you were to call it with cURL and store the cookie somwehre).
You get no $_GET or $_POST - instead you need to use $args - but if you're not passing in any variables this isn't really relevant to you.
You'll obviously need to ensure that you've given your script execute permission, otherwise, call it using /path/to/php /path/to/file.php.
Take a look at these resources, they should help you out.
http://www.php.net/manual/en/features.commandline.differences.php
http://www.php.net/manual/en/features.commandline.php
When you execute php from the command line (CLI) you enter another environement.
There are multiple differences
Some of them are
Configuration
When executing from command line, you run another php.ini file. (Usually /etc/php5/php.ini)
Permissions
On the web, you usually execute PHP with www-data user.
When executing from command line, script will be executed as the current user.
Arguments
You do not have access to $_GET, $_POST, $_FILES... superglobals anymore.
Instead, you have new superglobals so as $argv and $argc
Sessions
You do not have access to session nor $_COOKIES
Relative Paths
The path that the script uses is relative from where the command started
So, careful when using relative ./ path in your script
Eg:
$ pwd
/path/to/project/
$ cat app/script.php
<?php
echo getcwd(), PHP_EOL;
$ php app/script.php
/path/to/project
$ cd app
$ php script.php
/path/to/project/app
You can get the current directory by using getcwd

Cronjob using CURL/WGET

I want to run a PHP script every 15 minutes using either CURL or WGET.
This PHP file is in a local folder:
/home/x/cron.php
How would I run this using CURL/WGET?
It doesn't work when I try to run
curl /home/x/cron.php
Thank you!
CURL and WGET are more adecuate for URLs like http://myhost.com/cron.php
When the script is offline, you would better run it using php CLI:
Ex:
php -q cron.php
Just do something like this:
/usr/bin/php /home/x/cron.php
cURL/wget is for HTTP actions. If your PHP script is on the same system, you don't want to load it over HTTP. (You can, of course, if it is accessible over HTTP, but I don't think that is what you want.) Just call it directly.
Alternatively, you can set the execute permission on your script and throw in a shebang line for PHP:
#!/usr/bin/php
Then, just put your PHP script in crontab directly.
If you're using CURL or WGET, I believe you'll need to pass in the path as a URL. If you want to run the php script on the command line, you'll need to use the the php CLI

Categories