I'm Working on a php app where I need to have a number count in a sql data base reset at the first of the month and at 12am everyday via PHP and MYSQL.
I have googled but found nothing. As far as I know PHP needs to be accessed by a client to be manipulated.
What is the best method of having the server do this on its own?
You can use crontab
* 0 * * * /path/to/my/script
You can use cronjobs. On windows 'scheduled tasks', on linux crontab. For linux:
crontab -e
A line have to look like this to be called every day 0 am:
* 0 * * * /bin/php -q /script/path
Or, to call the file via web but do not get the content:
* 0 * * * wget --spider "http://url.com/cron.php"
If you do not have a dedicated server, check your webspace control panel for this or ask your webhoster.
I wrote a PHP script that push a CSV file into a database. I want to do this automatically every minute. I know there is a way via cron on Linux but I don't know anything about bash and think cron can't give my PHP file a callback, so I can show a progress bar for the user to see the timer interval. What do I do?
You can enter your jobs using crontab -e. If your default editor is vi, I recommend to change it nano using export EDITOR=nano because it is easy to use for starters.
Every line of the crontab file represents a job. The first 5 tokens are : minute, hour, day of month, month, day of week respectively, the last one is command so in your case first 5 tokens will be * * * * * that means run this job every minutes when the second is '00'.
You can call your php files directly using this command : php /var/www/cron.php & or using a browser wget -O /dev/null http://example.com/cron.php If you use first one you cannot use some $_SERVER variables but if you use second one, it is like a real browser.
In your case you can use like this :
* * * * * wget -O /dev/null http://example.com/cron.php
to add a cron and make it run every minute, type crontab -e and add the following line
* * * * * command you need executing
example:
* * * * * ls -l /home/ > /usr/local/users.txt
* * * * * df -h > /tmp/filesystem_usage.txt
* * * * * service httpd restart
Look at this for a starter: http://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/
Also remember that cronjobs don't support all $_SERVER vars like 'DOCUMENT_ROOT' and 'HTTP_HOST', so try to avoid them, or use a workaround.
Some 'callback' possibilities:
- Let your script trigger another script
- Redirect the output of your cron to an another bash script
- ...
I need to run a php file on every 2 hours, so i am using this command
* */2 * * * /usr/bin/php /var/www/html/sports/webservices/rss-insert.php
i am also using this
* */2 * * * php /var/www/html/sports/webservices/rss-insert.php
But both are not working. can anyone help me.
Thanks
The crontab you have made will run every minute of every second hour rather than every two hours.
In order to get it to run every two hours you need something like this:
5 */2 * * * /usr/bin/php /var/www/html/sports/webservices/rss-insert.php
which will run it 5 minutes after every second hour e.g 2:05, 4:05...
This is assuming your script is able to run. Try running the command portion from the cron by hand at the command line and make sure it does what you want.
This works for me definitely work for you.
0 0-23/2 * * * php /var/www/html/sports/webservices/rss-insert.php
I have built one php file to check some result, so that I need to setup a cronjob.
I set one to run every 30 minute, so that the results will be send. However, I don't know why my crontab did not run after every 30 minute.
Here is how I set the crontab:
*/30 * * * * php /var/www/html/result.php
I have confirmed my file directory is correct. What I not sure is about the timing part: isn't it possible to use */30 * * * * or 30 * * * * ? I set */30 * * * * and did not work.
Given
*/30 * * * * php /var/www/html/result.php
There are multiple possibilities why it is not working:
First of all it is important to check if the simple execution of php /var/www/html/result.php. This is required. But unfortunately, accomplishing this does not mean that the problem is solved.
The path of the php binary has to be added.
*/30 * * * * php /var/www/html/result.php
to be changed to
*/30 * * * * /usr/bin/php /var/www/html/result.php
or whatever coming from which php.
Check the permission of the script to the user running the crontab.
Give execution permission to the file: chmod +x file. And make sure the crontab is launched by a user having rights to execute the script. Also check if the user can access the directory in which the file is located.
To be safer, you can also add the php path in the top of the script, such as:
#!/usr/bin/php -q
<?php
...
?>
Make sure the user has rights to use crontab. Check if he is in the /etc/cron.d/deny file. Also, make a basic test to see if it is a crontanb or php problem.
* * * * * touch /tmp/hello
Output the result of the script to a log file, as William Niu suggested.
*/30 * * * * /usr/bin/php /var/www/html/result.php > /tmp/result
Use the -f option to execute the script:
*/30 * * * * /usr/bin/php -f /var/www/html/result.php > /tmp/result
Make sure the format in crontab is correct. You can do so for example using the site Crontab.guru.
To sum up, there are many possible reasons. One of them should solve the problem.
It may be because php is not in the path. crontab has a very minimal path. So, include the full path for your php program.
you can test your cron commands by piping the output to a file, e.g.
*/30 * * * * php /var/www/html/result.php > /tmp/result.log
From this reference page, under "Crontab Environment":
cron invokes the command from the user’s HOME directory with the
shell, (/usr/bin/sh). cron supplies a default environment for every
shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
Also, /30 syntax might not be supported by all platforms, so, try to change it to 0,30 instead.
Had a similar issue; from command line, it worked, but from cron, no go.
had a "include ("./connect.php"); in my php code for the db stuff.
Removed that, and added the connect.php code directly into the php script, and it worked from cron.
I had a similar issue on Ubuntu 14.04.1 and the problem turned out to be the way I was modifying the crontab:
I was using sudo crontab -e instead of just crontab -e and this caused my changes to be ignored.
I had a funny one regarding this. Although my scripts would run manually, they wouldn't run from crontab.
Turns out that because the script was being run from /usr/bin/php rather that the location of the file (as it does when I run it manually) my php require wasn't finding the files I wanted. Changing that to reflect the full address fixed it.
troubleshooting by running the script as /usr/bin/php -f /var/www/myfile.php helped me find the issue
You can use */30 * * * * wget http://my.domain.com/path/to/php/result.php
But Crontab executes the task using the current user that ran crontab -e. When you use wget it’s handled by Apache using the www-data user/group pair
First, make sure the script works as expected.
$ php /var/www/html/result.php
Second, edit the crontab for the Apache user account
$ sudo crontab -u www-data -e
or
$ sudo crontab -u root -e
Now add the crontab and output to a log file.
*/30 * * * * php /var/www/html/result.php > /tmp/result.log
After a day of puzzling why my script would work directly (to send data in an email to a gmail account) I discovered that all the deliberate sends worked when I clicked the url and all the cron sends went into spam. No idea why but I thought I'd share it.
Willem's answer showed me the way. In my case, I have a "include("connection.php")" inside my code. I changed connection.php to /my/full/path/connection.php. I have some rename() calls with the relative path, and I changed to the absolute path. That worked for me. I hope it can help someone else.
Easy and logical way:
Checking the cron logs at /var/log/cron will give you very useful info
less /var/log/cron
Eg.,
My cron entry is * * * * * /usr/bin/php /cat.php <== Run cat.php every minute
The log file will contain an entry similar to the one below every time a cron entry is run
Jan 24 08:06:01 OlaTower CROND[13641]: (root) CMD (/usr/bin/php /cat.php)
Jan 24 08:07:01 OlaTower CROND[13641]: (root) CMD (/usr/bin/php /cat.php)
Here, the php command will be executed every minute and there will be an entry in the log file every minute
If the entry is not there then crond is not even picking that cronjob. If the log entry is there and still you are not getting the desired output then there is something wrong with the command/application logic
Are you sure it is not running? If you use exec, realize that you are running from cron and the full path for everything is required, so instead of cp, you'll need to use /bin/cp.
Centos 7
For the record (and it could work for other distros)
I had the next script
* * * * * /usr/bin/php -f /var/www/html/cron.php >/tmp/result.txt
But it failed to execute.
In the /var/log/cron log file, I found the next line
crond[2213]: (/usr/bin/php) ERROR (getpwnam() failed)
What is that?
It's simple, the syntax of corn is * * * * * user command (check user)
* * * * * someuser /usr/bin/php -f /var/www/html/cron.php >/tmp/result.txt
Using Ubuntu w/ Vesta :
The following command works perfect for me,
/usr/bin/php /home/admin/web/mydomain.com/public_html/mycode.php
Feel free to comment if you have any question, have a nice day :)
I was stuck too. I am using centos 7 and had to run few php scripts. I initially tried this
$crontab -e
& inserted the scripts to be executed at 12 midnight.
0 0 * * * usr/bin/php /var/www/html/cronjob/myscript.php
However in var /var/spool/mail/centos, it gave me an error in the mail there
/bin/sh: usr/bin/php: No such file or directory
So then I used wget like this,
$ crontab -e
0 0 * * * wget https://myapplicationurl/var/www/html/cronjob/myscript.php
This also gave me an error.
ERROR 404: Not Found.
Then I realized my mistake of specifying the folder, since the url will already be pointing to html folder, the folder from there i to be specified, like this
0 0 * * * wget http://myapplicationurl/cronjob/myscript.php
and it worked !!!
Hope this helps any newbie like me :)
if you php script has an include or require, you must provide the full path yours includes
wrong way
// relative path
require_once("../library/PHPMailer/PHPMailerAutoload.php");
Correct Way
// full path
require_once("/home/bitnami/library/PHPMailer/PHPMailerAutoload.php");
I had same problem with my php. Then I test execute php from root dir:
php -f /var/www/html/my_proj_folder/test.php
and got some errors regarding path to lib (included files), such as parse_ini with argument 'config.ini' <- has been taken from my global lib and lose path when it has been started from root.
So,
try to run your php-file (php -f your.file)
check relative path and try to run with absolute path
check permissions to your.php - it has to have executable flag x (you can see it ls -l your.php and set by chmod +x your.php)
put #!/usr/bin/php -q before <?php in your main/executable file
I'm using PHP and was wondering how can i run my PHP script to run at midnight and check if a member has been inactive for six months if so delete.
See Crontab: http://en.wikipedia.org/wiki/Cron
example crontab entry for 12:01 am:
1 0 * * * /usr/bin/php /home/whatever/myscript.php > /home/whatever/outlog.txt
command to edit the crontab entries for the logged in user:
crontab -e
A cron line like the following should suffice:
0 0 * * * /usr/bin/php /path/to/script
I would advise that you run the script at an irregular time (i.e not on an hour or day boundary), as you may get to the situation when you have too many scheduled tasks occurring at the same time.
If the script generates any output, then that will typically be emailed to the user mailbox. So, you should design your script to not generate output in the normal case. Or, alternatively redirect the output to a file or the syslog.
I use syslog redirection quite a lot. For example the following will log the commands output into the syslog using the tag my_script.
20 1 * * * /usr/bin/php /path/to/script | /usr/bin/logger -t my_script