crontab, php: php script runs from command line, but not from crontab - php

I have verified that my php script runs from the command line, but it has not been executed from the crontab (yet, I guess). Does it take a while for it to start working?
this is the crontab line:
00,15,30,45 * * * * php /var/www/download.php
I want it to execute everyday, every fifteen minutes starting at the top of the hour.

Most likely it is environment variable issue. First thing to check is whether cron user has php in its path?
Cron jobs don't have access to all the env variables set in user's profile. Better to redirect stdout and stderr to a file in your cron command like this:
*/15 * * * * php /var/www/download.php > $HOME/cron.out 2>&1
And then after 15 minutes examine $HOME/cron.out why it failed.

*/15 * * * * php /var/www/download.php
if that fails, then your script is probably failing. permissions and what not. check the cron log.

Related

PHP Cron Job command

I'm using whm/cpanel and the first time i'm doing a cron job.
I have a php file located in /home/my_website/public_html/wp-content/youtube_channels/cron.php
I want this to run every minute.
I have this as a cron command but i want to make sure it is correct before going forward.
***** /home/my_website/public_html/wp-content/youtube_channels/cron.php
Is this correct? I want this code to run every minute.
A crontab line
* * * * * php /path/to/phpfile.php >> /path/to/logfile.log 2>&1
* * * * * time settings
php to execute an php file, You can not just call the phpfile like an executeable file (only if you setup and add #!/usr/bin/php correctly)
>> pipes the output from the phpfile into logfile (or else)
/path/to/logfile.log the log file
2>&1 ensures that all what is comeing from STDOUT and STDERR is piped into the logfile.
More info:
https://corenominal.org/2016/05/12/howto-setup-a-crontab-file/
Run a PHP file in a cron job using CPanel (not only for CPanel user)
http://php.net/manual/en/wrappers.php.php (STDOUT/STDERR)
Have a nice cron
For a crontab entry, you need spaces between the stars -
* * * * * /home/my_website/public_html/wp-content/youtube_channels/cron.php

How to run cron job in php

I have php function that I wnat to run every 15 minutes. I found some questions like this:
How can I make a cron to execute a php script?
and in my case I should use this:
*/15 * * * * /usr/local/bin/php -q /path/to/my/file.p
But should I run this command in terminal or put it in my file? And once, it is executed, will it run all the time or will have time limit?
Thanks!
PHP doesn't run cron jobs, your server (or operating system) is doing this. There are two ways to put your cron job to work:
#1
Using the shell command crontab. The command crontab -l will list all existing cronjobs for your user (most likely there are none yet). crontab -e will open an editor window where you can put in a you cron job as a new line. Save, and your cron job is now running. crontab -l again and you will see it listet. crontab -r to remove all the cont jobs.
You can also start a cron job from a file. Simply type crontab filename (eg. crontab textfile.txt)
Alternatively you can also start it from within PHP. Just put your cron job into a file and start it via exec() like so:
file_put_contents( 'textfile.txt', '*/15 * * * * /usr/local/bin/php -q /path/to/my/file.php' );
exec( 'crontab textfile.txt' );
#2
If you have admin privileged on your system you can create a file in /etc/cron.d/ (for example, call it my_cronjob) and put your cron job there. In this case you probably want to run it as a user (not as admin, that would be rather insecure). This is quite easy to do, just add the user name like so:
*/15 * * * * user_name /usr/local/bin/php -q /path/to/my/file.p
(In this case the cron job will not be listet under crontab -l)
Answering your second question: As long as the cron job is listet in crontab -l or as long as the file is sitting in /etc/cron.d the cron job is running, in your case, every 15 minutes.
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script. More on this later.
Please read this tutorial http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

Why this script is working but my cron isn't?

I try to create a cron on a server (I should have right on it).
This is my script cron.php :
<?php
echo "CRON OK \n";
?>
I connect with SSH and create the following cron with crontab -e :
* * * * * php /home/myname/www/cron.php
But I don't know how to save it.. Anyway I save the crontab at the default location /tmp/crontab.XAblsdZ/crontab, the server tell me "crontab: installing new crontab" and when I execute crontab -l I can see * * * * * php /home/myname/www/cron.php.
I also try my script : php /home/myname/www/cron.php
The server display "CRON OK" so I guess it works.
But without this command I can't see any "CRON OK" displayed... Am I missing something ? How to enable this cron and execute my script every minute?
either add #!/usr/bin/php as suggested or you could add it to your crontab, make sure the /usr/bin/php is actually the right path to your PHP.
* * * * * /usr/bin/php /home/myname/www/cron.php
The cron line looks good to execute every minute though.
Try adding this to the beginning of your cron.php:
#!/usr/bin/php

PHP Cronjob does not execute

I am running Ubunutu Linux server, PHP5, Apache2 and am having trouble getting any sort of cronjob to run through the crontab.
I edit the crontab using
crontab -e
I save the file I want to run:
*/5 * * * * php /home/user/public_html/crx/cronx.php
it saves fine. I can run the file from the console and goes through fine. I can't even find any existing logs for the file. I checked cron was running, stopped and started... no change.
The current php file is just a simple test script that inserts a single line into a database.
I checked the permissions for the file and has read and write. Am absolutely stumped. I can't seem to get ANYTHING to run through cron. Is there something I can run to test permissions?
EDIT
I have also tried the following command
/usr/bin/php /home/user/public_html/crx/cronx.php
I used whereis php and which php to locate and confirm it is all running in the right area
You have too many * values for your times.
Also, cron may not have a PATH set up correctly to use PHP.
Instead try:
*/5 * * * * /usr/bin/php /home/user/public_html/crx/cronx.php
Where /usr/bin/php is the actual path to PHP. From the console you can run which php to see the path to the PHP binary you should use.
EDIT: Here are a couple of more things to try in order to troubleshoot:
# see if cron is running just by having it create a file
*/5 * * * * touch /tmp/crontab-$(date +%s)
Another option:
Set the permissions of your PHP script to 755, and change the beginning to:
#!/usr/bin/php -q
<?php
// rest of script
Then change your cron tab to:
*/5 * * * * /home/user/public_html/crx/cronx.php
I'm still not sure if cron is the issue or the running of the PHP script.

How do I use cron to run a database cleaning script?

I have a database with a bunch of links that I want to keep updated. Basically if a link returns a 404 error code I want to remove it from the database. I have a script that I am using however I need to run it manually. How can I make this work using CRON?
in your shell as cron user (or root):
crontab -e
This will bring up your crontab file in your editor. Add a new line something like this:
* */12 * * * /path/to/script
Save/exit the file.
Now for a quick lesson on cronjobs:
-The first 5 arguments in the line tell how often, or when the cron daemon will execute the 6th argument.
-From left-right, arguments represent: minutes, hours, days, weeks, months
-An asterix (*) tells the cron to run on all values of it's associated time measurement (example * * * * * means to run every minute, of every hour, of every week, and of every month!)
In my example, * */12 * * * means to run every 12 hours.
Check out: http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/
To run a PHP script with cron you can use the PHP executable and the path to the script.
On most linux systems you want to edit your cron file (the crontab) with the command crontab -e. This will open up a command line based editor and you can just append your new job to the bottom of the file using this format.
<minute> <hour> <day_of_month> <month> <day_of_week> php /path/to/script
If the commands dont work for you let me know what distribution you are using and I can modify the instructions.
/usr/bin/php -q /home/user/public_html/script.php

Categories