Cron job on Ubuntu for php

I use Ubuntu on the server and I use Putty for access. I want to create cronjobs for my php site. How can i do this?

+6
php cron ubuntu
source share
2 answers

If you want your php site to perform some common tasks, there are two possible ways.

1) You use cron to regularly search for a specific page. You can do this using a text browser, for example. lynx. You pull your script like this:

* * * * * /usr/bin/lynx http://yourhost.com/cron.php -dump > /dev/null

(This will call every minute. This way you can create your own schedule inside your application)

2) You invoke your script using the php command line interpreter:

* * * * * /usr/bin/php /path/to/cron.php > /dev/null

In general, solution two is better. However, you will need access to the box. Cron in the solution can be launched from another host if you cannot install the crane on the host.

Also be careful with a common error when using the php command line version. On debian (and possibly other systems) there may be separate php.ini files for cgi, cli and mod_php. If you configured your configuration, make sure the php command line is using the correct one. You can check this with:

/usr/bin/php -i | less

In response to a dimo comment, I did some tests. I called a simple local php script (which is only an echo test) 1000 times with lynx, wget and php-cli:

 kbsilver:temp kbeyer$ time . wget.sh real 0m14.223s user 0m2.906s sys 0m6.335s (Command: wget -O /dev/null "localhost/test.php"; 2> /dev/null) kbsilver:temp kbeyer$ time . lynx.sh real 0m26.511s user 0m5.789s sys 0m9.467s (Command: lynx -dump "localhost/test.php"; > /dev/null) kbsilver:temp kbeyer$ time . php_cli.sh real 0m54.617s user 0m28.704s sys 0m18.403s (Command: /opt/local/bin/php /www/htdocs/test.php > /dev/null) 

lighttpd server, php(fastcgi) with apc (on Mac OS X).

It turns out, really, wget is the best tool for working with respect to speed.

Thus, the result of php-cli not such a surprise, as other methods reuse the already running php stream with the operation code cache.

Thus, the only real benefit of using php-cli is security, since the script will not be accessible externally, since you can push it outside of docroot.

(This test is obviously not 100% accurate, but the differences, in my opinion, are obvious)

+15
source share

I suppose you want to backup your PHP site? Edit crontab using:

 crontab -e 

This will launch a vi instance where you can edit crontab, press i for insert mode. Then you need to enter information when cron recording will be performed, and a command to start at this time, for example:

 30 10 * * * tar -zcvf ./myphpsite.tar.gz /var/www/phpsite 

So the command above will tar gzip your phpsite to / var / www / phpsite at 22:30 every day. Exit and exit vi with : wq

See additional information:

http://www.adminschoice.com/docs/crontab.htm

+1
source share

All Articles