New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
cron job is not updating the database
Hi,
I created a php file and when i am running the file through browser it is providing the output and update the database, i run the file through terminal and it is working fine.
when i run the file through cron it is not creating any error, but also not updating the database entries as well.
can anybody help to resolve this issue.
Comments
Check the logs to see if the cron is even running.
Could you post the contents of your cron file pertaining to the php file ?
I do check and the cron is running.
the cron entry is */5 * * * * php -q /path/server/status/getserverstatus.php
If error reporting is enabled then try
php -q /path/server/status/getserverstatus.php
In command line. You'll get your error.
or enable error reporting by error_report(E_ALL);
when i am going to run the php file through browser the database updated and the update query is in the php file.
when i run the same file from the terminal the database entries updated successfully. but when i am trying to run the file through cronjob the job run but the db is not updating the values.
I run the command through terminal and it is working fine. that's why i list in my previous comment. the db is also updating the value when i run through terminal. but it is not going to update the db records when it is running through cron.
Could you try , which php or where php or try with php5 you might get an output like
/usr/bin/php or /usr/bin/php5
Now use that to make your cron job look like
*/5 * * * * /usr/sbin/php5 -q /path/server/status/getserverstatus.php
or
*/5 * * * * /usr/sbin/php -q /path/server/status/getserverstatus.php
What's about ownership (chown) and permissions (chmod) of cron file?.
it is root:root
i have change it to the web user but no success. i did one more thing. i change the script and push the output to a txt file.
where i get the following error.
PHP Warning: require(..f/init.php): failed to open stream: No such file or directory in /var/myserver/getserverstatus.php
Try adding cd /var/myserver/; before the php command.
Do one thing
if(PHP_SAPI == 'cli' && $_SERVER['REMOTE_ADDR'] == '127.0.0.1')
{
//code here
}
use that and then use
*/5 * * * * /usr/bin/GET http://localhost/path/to/whatever/blabla.php
In a script to be run by cron, always use absolute paths. Cron does not have the same environment or path settings that you have as a user.
Create /tmp/cron.sh file with content:
#!/bin/sh
cd /path/server/status/
exec php -q getserverstatus.php
chmod to 755 and then create cron with content '*/5 * * * * /tmp/cron.sh
Why would the OP create a cron to a script in /tmp? It's ephemeral..
Like others have said, use absolute paths.
Also, @AIC, unless you need the script to run with root privileges, it's a best practice to run with only the privileges that you need (e.g. www-data, or some other user). And as @BharatB suggested, if the script should only be run from CLI, it's a good idea to check that right at the beginning before doing anything.
The reason why in /tmp because permissions always 1777 and then ownership unnecessary.
Right. But it's tmp. One should never rely on things to be there, that's why it's called tmp (i.e. temporary).
Plus, do you see anything inherently insecure about running scripts from a location like that? Some people even mount /tmp with noexec, so the solution wouldn't work under those situations.
Notwithstanding, you're advising the OP to workaround a permission/ownership issue when it hasn't even been established that that is the cause. Even if it was, the correct solution is to fix the permissions and ownership, not workaround the issue... And it likely isn't an issue since the OP was running the cron job as root.
This
Eliminate other factors, test the code and then if running well, move the code to other safe place. OK?.
I know about secure and it's my concern for my Kloxo-MR.
If not used in "crontab", you may need to put a username before your command:
Try this :
If it doesn't work, try this:
Keep the
it means source
You could always run it via curl:
* * * * * curl --silent "http://status.example.com/cron.php" >/dev/null 2>&1
Updated: Fixed time thanks to @TinyTunnel_Tom
Why */1? Just * is enough
My bad, I changed it from */20.
You are right, cron doesn't take the relative path. I have provided the absolute path and then it run correctly.
I did all the options before changing to absolute path.
change the permission, change the owner of all the include files, change the cron to my webuser, check the time stamp of cron and php etc.
None of them were successful.
So the last option was i open the serverstatus.php file and change the requireonce (../init.php) to requireonce(/path/to/my/init.php);
Thanks for help everyone.
The issue is resolved now.
Glad to hear you got it working.
If hard-coding the path becomes a nuisance for portability, you can have a look at PHP's
getcwd()
anddirname(__FILE__)
. There's a nice explanation and comparison here.Thanks i will look into this.
You can set PATH in your crontab file.
Another useful thing you should always set is MAILTO. I suspect every time this failed, mail was generated to someone...if it had been you, you would have seen the error immediately.