How to use Crontab in linux


Posted on 2014-11-29 09:20:18


Structure setting Crontab

*     *     *     *     *     Five stars can replace by interge

-     -     -     -     -

|     |     |     |     |

|     |     |     |     +—– day in week (0 – 6) (sunday = 0)

|     |     |     +——- month (1 – 12)

|     |     +——— day (1 – 31)

|     +———– hour (0 – 23)

+————- minute (0 – 59)

Some commands line:

crontab -e: Create or modify file crontab
crontab -l: Show file crontab
crontab -r: Delete file crontab

service crond start: Run service cron
service crond stop: Stop service cron
service crond restart: Restart service cron

Example

50 15 * * 1-5 : execute_file From Monday to Friday 15h50p → run execute_file */2 * * * * execute_file  : two minutes run file execute_file

Run file php use Crontab

* * * * * /usr/bin/php -q run.php >> /var/www/html/cron.log

/usr/bin/php : Url to php file of linux system
run.php : Php file for run 
/var/www/html/cron.log : write to log

Note: If the first colum in file .php which has same behind code. You can not report /usr/bin/php -q in setting crontab

#!/usr/bin/php -q
<?php
 
// php code here
 
?>

Run a file shell of cakephp framework with crontab

* * * * * /var/www/html/demo/cake/console/cake -app /var/www/html/demo/app run >> /var/www/html/cron.log
demo is project cake /var/www/html/demo/cake/console/cake :   Url to file cake linux of cakephp -app /var/www/html/demo/app : Set again url to main app run : is file shell of cake (run.php) /var/www/html/cron.log : write to log

Note: - file /var/www/html/demo/cake/console/cake  must set permission to 755 - file /var/www/html/cron.log must set permission to 777   Author: Duynb Translator: Hainq