Wednesday, 3 April 2013

Creating cron jobs

                Cron is the time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule. The crontab files are stored where the lists of jobs and other instructions to the cron daemon are kept. Users can have their own individual crontab files and often there is a system wide crontab file (usually in /etc or a subdirectory of /etc) which only system administrators can edit.Here are the some of the common commands than are associated with the crontab:
crontab filename Install filename as your crontab file.
crontab -e Edit your crontab file.
crontab -l Show your crontab file.
crontab -r Remove your crontab file.
MAILTO=user@domain.com Emails the output to the specified address.

                     The fields are separated by spaces or tabs. The first five are integer patterns and the sixth is the command to be executed. The following table briefly describes each of the fields:
Field Value Description
minute 0-59 The exact minute that the command sequence executes.
hour 0-23 The hour of the day that the command sequence executes.
day 1-31 The day of the month that the command sequence executes.
month 1-12 The month of the year that the command sequence executes.
weekday 0-6 The day of the week that the command sequence executes. Sunday=0, Monday = 1, Tuesday = 2, and so forth.
command Special The complete command sequence variable that is to be executed.
Example:

1.     The following line specifies that the Apache error log is to be cleared at     one minute past midnight (00:01) of every day of the month, of every day of the week, assuming that the default shell for the cron user is Bourne shell compliant:


     1 0 * * * printf > /var/log/apache/error_log
 
2.      Run the script every day at 12:00. 

    0 12 * * * root /home/usr/path-to your-file

 

Creating a cron job with php :
       If you want to collaborate cron to work according your php script this can be easily achieved with 4 simple steps.
Step1 : Add these lines to your php script  "#!/usr/bin/php
Step2 : run crontab -e (if already created cron tab)or crontab filename (to create new cron tab) in your terminal. 
Step3 : Add the line like this "0 12 * * * php /home/public-web-folder/file.php" 
Step4 :  Save your crontab and exit.

Now all you have to do is to check your cron is running or not . Run "crontab -l"
to see whether  crontab is saved properly or not. If it is saved then shows the your cron command in queue.
   You can check whether your php script is running properly or not just copy from "php /home/public-web-folder/file.php" and run in the terminal to see the output.If it runs then your script is working or else correct the errors shown.

2 comments: