Jump to: navigation, search

Setup cron tab job

From w3cyberlearnings

Contents

how to setup cron job in ubuntu

Cron is a system daemon used for execute a specific tasks in the background at designated times.

to learn more about cron

root@ubuntu:~# man crontab

the file to be executed by cron has to be excutable

sophal@ubuntu:~$ chmod 755 filename.php
or
sophal@ubuntu:~$ chmod a+x filename.php

as root to edit crontab

root@ubuntu:~$ crontab -e

as non-root privileges to edit crontab

sophal@ubuntu:~$ sudo crontab -e

list cron job that are currently running on your system

sudo crontab -l

the time and date fields

 field        allowed values
-----          --------------
 minute         0-59
 hour           0-23
 day of month   1-31
 month          1-12 (or names, see below)
 day of week    0-7 (0 or 7 is Sun, or use names)

crontab syntax

  • first star is minutes
  • second star is hour
  • third star is day of month
  • four star is month
  • five star is day of week
* * * * * <command to execute>

output to log or ignore output send to email

cron to ignored out put

* * * * * php /home/pual/mycommand.php > /dev/null   

cron to send output to a mylog.cron file

* * * * * php /home/pual/mycommand.php > /home/pual/mylog.cron

cron to send out put but append to a log file

* * * * * php /home/pual/mycommand.php >> /home/pual/mylog.cron

run cron every minutes for every hour every day and every month

* * * * * php /home/pual/mycommand.php > /dev/null

run cron every hours

0 * * * * php /home/pual/mycommand.php > /dev/null

run cron every hour at minutes 15 (ie. 1:15, 2:15, etc..)

15 * * * * php /home/pual/mycommand.php > /dev/null

run cron once a day every 11:30 pm

30 23 * * * php /home/pual/mycommand.php > /dev/null

run cron once a day every 11:30 am

30 11 * * * php /home/pual/mycommand.php > /dev/null

run cron once a month every first day of month at 12:00 am (ie. 1/1/12, 2/1/12, 3/1/12...,8/1/12)

0 0 1 * * php /home/pual/mycommand.php > /dev/null

run cron on Monday for every month between the date of 8 to 18, at 3:30 a.m.

30 3 8-18 * 1 php /home/pual/mycommand.php > /dev/null

run cron every Friday at 1 a.m

0 1 * * 5 php /home/pual/mycommand.php > /dev/null

run cron every Monday to Friday at 1 a.m

0 1 * * 1-5 php /home/pual/mycommand.php > /dev/null

run crontab every 10 minutes for myreport.php and send the result to /dev/null

*/10 * * * * php /var/myreport/myreport.php > /dev/null

run crontab every 30 minutes for myreport.php

*/30 * * * * php /var/myreport/myreport.php > /dev/null

run crontab every minutes 5, 10, 15, 20, 25, 30, 40, and 50 minutes

  */5 * * * * command
  */10 * * * * command
  */15 * * * * command
  */20 * * * * command
  */25 * * * * command
  */30 * * * * command
  */40 * * * * command
  */50 * * * * command

run cron at every day at 7:30 a.m and generate output to log file with date

30 7 * * * php /var/myreport/myreport.php >> /var/myreport/mylog_cron-`date+\%m\%d\%y`.log

run cron at every day at 7:30 a.m and generate output to send to specific email

30 7 * * * php /var/myreport/myreport.php | mail -s "cron job activity" [email protected]

cron offers some special strings

 string         meaning
 ------         -------
 @reboot        Run once, at startup.
 @yearly        Run once a year, "0 0 1 1 *".
 @annually      (same as @yearly)
 @monthly       Run once a month, "0 0 1 * *".
 @weekly        Run once a week, "0 0 * * 0".
 @daily         Run once a day, "0 0 * * *".
 @midnight      (same as @daily)
 @hourly        Run once an hour, "0 * * * *".

usage of special strings cron

execute a myexecute1 when the system restart

 @reboot /path/to/myexecute1

execute a myexecute2.php when the system restart

 @reboot php /path/to/myexecute2.php 
Navigation
Web
SQL
MISC
References