Test if a cron is executed only by the server [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In php, how to detect the execution is from CLI mode or through browser?
How to test if a cron is executed only by the server
Thanks

Cron will never put anything on the display unless you use something like 'wall' in your cron script.
Are you redirecting your output to a log file or anything?
What you can do is to add a line at the bottom of the script you are executing in the cron; that does something like:
date +"%D %r `echo Cron completed`" >> /tmp/cron_job.log
Then you could check
cat /tmp/cron_job.log
and it would tell you when it finished.

Related

I want one php file to protect from normal user but let it to be used by cron job [duplicate]

This question already has answers here:
Run a script.php on cron job on linux/apache server but restrict public access to the php file
(4 answers)
Closed 5 years ago.
I am using ooowebhost. I have a PHP file, which I want that normal user should not be able to run it except cron job.
As this file is inside public folder so it hard to protect from normal user but let cron job executor to access it.
you can block your script executing from browser, but run through cron/cli.
Method 1:
Add below line in the start of the script,
if (php_sapi_name() !='cli') exit;
OR
Method 2:
Add in the start of the script,
if ($_SERVER['HTTP_USER_AGENT'] != 'yourSecretAgent') exit;
replace agent ,
Thanks,
M.AkberKhan

How to differentiate between a CRON job and a human user in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - how to best determine if the current invocation is from CLI or web server?
I created a daily CRON job on my hosting server, which runs on UNIX, of course.
I put the following command in:
/usr/bin/php /home/myusername/public_html/foo/foo.php
And then, as expected, it executed this foo.php on a daily basis.
But this foo.php contains important information, and I don't want random people (not to be rude) going to http://www.mywebsite.com/foo/foo.php and executing the script.
So what can I do to differentiate between the CRON job, and a human user in PHP?
I've recently seen that when the CRON job is executed, no IP address is given ($_SERVER['REMOTE_ADDR'] is empty). But I'm not sure if that's a fluke.
I tried searching for this topic on Google, with no avail.
You could just put the cron script outside of the web root, say in /home/myusername/cron.
Alternatively, if this is not an option due to FTP restrictions, you can add a parameter to the cron script:
/usr/bin/php /home/myusername/....../foo.php cron
Then check:
if( $_SERVER['argv'][0] != "cron") die("This is a cron script, you cannot access it.");
I've used this in the past
if (php_sapi_name() != "cli") {
throw new Exception("someone tried to run this script outside of cli");
}
The php_sapi_name() function can tell you who's running the script. Here's the doc page: php_sapi_name

Keep loading my script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
schedule an email in php
How can I keep loading a PHP file on my server after every hour?
It will mail me something. Actually it mails me something when I visit it. But I want it to mail me after every hour. So someone should either visit it each hour or I have to use cron job like something.
Any help will be appreciated.
You should use a cronjob.
Start by opening you terminal and run
crontab -e
You may need to configure your crontab settings (default editor) if this is the first time you are using crontab. Now, in your editor, you have to call your php script like this: (it is set to be called each hour)
0 * * * * /path/to/php /path/to/your/script.php
You can also use an alias of hourly.
#hourly /path/to/php /path/to/your/script.php

How to run a background process in PHP and get the return value [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Make PHP wait for Matlab script to finish executing
Okay, starting from php execute a background process
to run a background process works great. The problem is, I need the return of that process also. The obvious solution to me is :
$cmd = "($cmd > $outputfile 2>&1 || echo $? > $returnfile) & echo $! > $pidfile";
exec($cmd);
When I run the generated command on the command line, it backgrounds and the files are filled out as expected. The problem is that when php exec() runs, the command doesn't go to the background (at least, exec doesn't return until the command finishes). I tried variations with nohup and wait $pid, but still no solution.
Any thoughts?
This is tricky- you could potentially fork the process to do something else, leaving the original process in place.
http://php.net/manual/en/function.pcntl-fork.php
However, if this is a web application, there's no built-in way to retrieve the return code or STDOUT back into the parent process since it's technically async (your request-response cycle will likely end before a result can be produced).
You could store the return code and / or STDOUT to files to check later, though.

php exec in background not working [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Call another PHP script and return control to user before the other script completes
Hello,
I'm executing this command in php:
exec('php /path/to/script.php one_argument_passed &");
But it's not executing it in the background. Is there a setting or something in php I need to do to make this work?
Maybe you have a permissions problem.

Categories