script runs fine when run from command line but not via cron - php

i have a php script which runs fine when executed by SSHing into my server and running it.
however the php script doesn't seem to run even though a cron job set to run it every 10 minutes.
How do I view errors produced by cron job ?

Remember that cron runs your script in a different working directory than what you're probably used to. Try something like the following on the command line:
cd /
./path/to/your/script.php
If it fails, modify all paths in your script correctly until it runs. Then it will run in cron.

Sometimes, the error messages are emailed to you by cron. Otherwise, it probably gets sent to a hapless system administrator who probably scrupulously saves the messages in /dev/null.
When things work from the command line and do not work from cron, the 'environment' is almost always at fault. Something that is set in the normal command line environment is not set in the cron environment. Remember, cron does not run your profile for you - you have a bare minimal set of environment variables with bare minimum values for things like PATH.
Generally, I assume that I should run a shell script from cron, and that shell script is responsible for ensuring the correct environment is in place - and that errors are trapped and logged appropriately - before running the main part of the software.

not sure but it's probably credentials related. Schedule your task to run with your user ID and see if that doesn't clear it up. If so, you'll need to create a set of "batch" credentials that you can use to schedule tasks to run beneath.

Related

How to run a cron job without using exec or system function in PHP?

Gist
I am trying to run a cron job in PHP. But the exec and system functions didn't work. (Because the server administrator doesn't allow me to use these two functions due to the security reasons) However, I still need to use PHP to run it, because the running time is decided by the formula and it's uncertain. As a result, how can I run the cron job without using exec or system function in PHP?
My thought
The only way I have come up with is adding the cron job via the cPanel, which is set to “once per minute.” That means setup a cron job for every minute to check the target PHP page whether it has anything to do at that moment.
Issue
There's a possible issue if it always checks the page every minute per day, won't it damage the host? (Maybe it will cause a damage to CPU or maybe occupy the memory space, etc.) If so, how to improve it?
You can call a bash script containing a call to PHP-CLI and then the file:
#!/usr/bin/php
/path/to/my/php/file.php
Go to the command line of the server and type "which php". Replace /usr/bin/php above with that which is returned. Then you call this bash script in your CRON.
As you say you have a cpanel server, im guessing its a linux box. You run a cronjob from a linux box by typing crontab -e and then to run a php script. */5 * * * * /usr/bin/php /path/to/file.php to un it every 5 minutes.

how to run php file using cron jobs

I have a E-mail schedule runs everyday on php page using cron jobs. The php code workds fine when i run the page using a link.
Now when I run the php script using cron jobs, it also works fine but when I put some query the cron jobs won't understand the link.
for example: http://www.wetube.org/cron.php?id=01001 so now if I try to run this everyday using cron job it's doesn't work.
But if we just erase the query it works fine. Do you guys know any code which makes this link work in cron job?
Cron runs commands as they would be ran via the shell, so running PHP would use local paths.
You need to use a command like:
php /home/USER/public_html/cron.php
Or if including the query string is necessary, use cURL instead (if it's installed):
curl http://www.wetube.org/cron.php?id=01001
You might want to look at not exposing your cron scripts to the internet - move them to outside your web directory because if someone finds it they can constantly reload it to spam your cron scripts (i.e. sending lots of emails)
I would add hash like
curl http://www.wetube.org/cron.php?id=01001&hash=cm349ucKuc023b2ynGyv23ycr23
and in php file
if(isset($_GET['hash']) && $_GET['hash']=='cm349ucKuc023b2ynGyv23ycr23'){
....
stuff to do
....
}
*you can even add specific time/date check when it should be run.
*you can check IP
*generate sha512 (I would recommend) hashes in both cron and php file with the same salt and maybe even time and then check if they are the same - it would be impossible for a hacker to recreate it - except if he somehow gets your original hash setup

How to start a exec() PHP command using a cron but not have 2 of them running?

I just can't figure this out.
I have a script that gets data from Facebook API and this script runs all the time. (using set_time_limit(0); )
However, sometimes the Facebook API gives errors and stops the script. Therefor, I would like to have a cron task every 5 minutes or so that checks to see if the script is still running and if not, starts it again.
I tried several things but it looks like I cannot run a exec() command from a cron job because of different user rights or something? How would you guys do this?
I use CentOS and PHP 5.3+
Set up the cron under a different user (say, root), which will get around any rights issues. However, PeeHaa makes a good point: if this is a cron script, there's no reason to use exec, as exec's job is to send commands out to the OS... these commands can be run directly from the crontab rather than having cron execute a php file.
You may want to look into creating a Daemon which is better suited to running a script continuously. You can create one using PHP with this PEAR package System_Daemon
If this process runs very frequently, run it in an endless loop and just sleep it. No need for crontabs.
while(true) {
//magical code stuff
sleep(60);
}

filesize function fails when run from cron

I get the below error when I run my scrip from cron
Warning: filesize() [function.filesize]: stat failed for /home2/sharingi/public_html/scrape/zip/dailydose/April_14_2011.zip in /home2/sharingi/public_html/scrape/zip/zip.php
However if I run the script from my browser it works fine. Some kind of a permissions problem?
It's probably an issue related to the user that your cron process runs under. Make sure that whatever cron runs as has permissions, since it's probably not the same user as your ssh account or the webserver account. You can probably figure out which user cron runs as by configuring cron to run the command whoami and email you the output.
If you can't figure out how to make that work, you might try configuring cron to wget the public url that you know works. Don't forget to turn off the file saving, and set it to quiet mode, otherwise you'll get a lot of garbage from each run.
if you're in a shared hosting environment, your cron job is probably running as your own user, so unless you yourself don't have read permissions for the file in question, I imagine that's probably not the issue.
As a probable work-around, in case you can't get to the bottom of it easily, here's a function which should allow you to get the info you need without using the php-builtin.

How can I set cron job through PHP script

How can I set cron job through PHP script.
This will add a script that runs every day at 9:30am.
exec('echo -e "`crontab -l`\n30 9 * * * /path/to/script" | crontab -');
You may run into problems with permissions if you are running this script from a web server. To get around this, I would suggest a different approach.
Here is one possible solution. Create a list of scripts that need to be run. You can save this in a text file or in a database. Create a script to read this list and run it every minute or every 5 minutes (using a cronjob). Your script will need to be smart enough to decide when to run the list of scripts and when to simply exit.
Do you know how to set a cron job normally? (outside of PHP, i.e. from a bash script or the command line).
If so, you just need to use the php function exec to issue the same commands you would have to create the cron job at the command line. One caveat is that there may be permission issues and you have to be really careful about what you put in that exec function (you don't want to pass input from the end user to that function).
You can't set a CRON job through PHP script, you have to set it one the server side. Unless you want to do it via system function, you can't set a CRON through php.
If you're not running on your own server and use hosting service, ask your hosting provider how to set up a CRON script (if the provider allows it).

Categories