Keep loading my script [duplicate] - php

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

Related

PHP fopen can not read file if called with cronjob [duplicate]

This question already has answers here:
Crontab - Run in directory
(2 answers)
Closed 8 years ago.
I have a php script that sends out a email report with an attached pdf document. I use fopen. If I call the php file manualy it works without any problems and the mails are beeing send as expected.
But:
If the same file should is call by cron job, it does not work at all. The error is:
Warning: fopen(filename.pdf): failed to open stream: No such file or directory in /home/public_html/util.php on line 29
Any ideas how to fix this?
Every cron runs in a minimal environment, which can be a different shell and a different profile than your user account. Also scripts are started not from the containing directory themselves.
In most cases it works, if you just change your directory to the one containing the script like you do when execute the script on the command line shell. Instead of
* * * * * /usr/bin/php /path/to/your/file.php
write this:
* * * * * cd /path/to/your/ && /usr/bin/php ./file.php
If this is not sufficient, you can load your default user shell and your PATH variable with writing
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
at the top of your crontab (with the right settings of course, which might be /bin/bash and the output of echo $PATH).
Cron jobs run without any environment, including a current directory. That means that you'll need to use the full path to any files that you want to work with.
If you add a valid email address at the top of your crontab:
MAILTO=your-email#example.com
you'll get any output the cron job does sent to your email. This includes error messages, which can be useful for debugging.

Unable to run php script using cron [duplicate]

This question already has answers here:
Cannot get PHP cron script to run
(4 answers)
Closed 8 years ago.
I am running a php script using cron. The name of the file is : PriceChecker.php .
This is my cron script
# m h dom mon dow command
0 07,11,16,20 * * * php /var/www/mainsite/PriceChecker.php
* * * * * /var/www/mainsite/pricecheck.sh
The shell script is:
#!/bin/bash
php PriceChecker.php >logger.log
The first line is the original. The second is for testing
I have tried various variations including: /usr/bin/php /var/www/mainsite/PriceChecker.php
If I log the out in the cron php /var/www/mainsite/PriceChecker.php > logfile.log
The logfile is created, but is empty
Same as here, I think Cannot get PHP cron script to run
You forgot to specify the user name you want your script to be run as.
Specify the full path of the PHP file to be execute
Okay what worked was CDing into the directory of the script
https://serverfault.com/questions/97828/php-from-command-line-path-problems/97881#97881

Cron Job in Laravel [duplicate]

This question already has answers here:
Cron Job with Laravel 4
(4 answers)
Closed 7 years ago.
I am trying to develop a cron job for a command I have already created. I am completely new to cron jobs so I dont really know how it works.
Trying the command by myself in the console works perfectly. All I need is to be able to execute it every 24 hours. I am using Laravel 4, can anyone help?
Thanks!
To create a cron job as root, edit your cron file:
[sudo] crontab -e
Add a new line at the end, every line is a cron job:
25 10 * * * php /var/www/<siteName>/artisan <command:name> <parameters>
This will execute the same command at 10:25AM everyday.
Just make sure you keep a blank line after the last one. And you also might need to use the full path of your php client:
25 10 * * * /usr/local/bin/php /var/www/<siteName>/artisan <command:name> <parameters>
You could register your cron job like this:
php /path/to/your/laravel/project/artisan your-custom-command
See my answer on this question, my example is for L3 but should work for Laravel 4 accordingly.

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

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.

How can I set up cron job in my site using PHP?

I want to mail after 6 hours automatically to my user who hasn't fully completed form on my website.
Help Me
Use crontab -e to edit the cron table for your account.
In the crontab, put an entry something like...
0,10,20,30,40,50 * * * * /usr/bin/wget -O - -q http://path.to/cron.handler.php
or the equivalent
*/10 * * * * /usr/bin/wget -O - -q http://path.to/cron.handler.php
...which will run the cron handler php file every 10 minutes using wget (there are other options as well, and you may need to edit the command appropriately). (Note: you don't want to just run it every 6 hours, because then if someone happened to fill out the form right after it ran, it wouldn't have been 6 hours since they filled it out next time it runs, so you'd end up with 10-11 hour gaps.)
Then in your PHP file, find users who BOTH (a) haven't fully completed the form for at least 6 hours and (b) haven't been emailed yet. Send them an email, and mark them as having been emailed.
You will need to create the php script that does the checking and mailing, and then set the cron job like the following
/path/to/php -q /home/username/public_html/mycheckingscript.php
Obviously you will need to adjust the first path to point to your php binary, and the second path to point to the full location of your checking & mailing script.
I don't think you want to set the cron up using php. Instead write a php script and then have cron execute that script every hour or so. This would be something that is going to be dependent on your operating system.
For linux, here is the manpage for using crontab.
There is no way you can change/add a schedule on the cron job on the fly. according to my experience. because until now i did not find..

Categories