Today's Question: 

Hide

Different programming are similar in helping people build staff work as people want them to. But they all have their own features which differentiate them from other programming languages. The language type may be different, for example Java is OOP, some may have different syntax. A programming motto usually can best describe the characteristic of the programming language. For example, "Write once Run everywhere" for Java.Do you know any other programming language motto?
Do you know the mottos of programmig languages?
Description My Answer

Technical Article => Operating System =>  Linux/Unix

Create cron job on CentOS

Source : Peter    Date : 2012-06-11 09:23:45  

These two days, I am building a website and deploying it on a VPS server which uses CentOS 5. I don't have cPanel or Plesk for my account, so I need to install and configure everything myself including Apache, PHP, MySQL and FTP server, also today's topic cron job. Since my website has a ranking algorithm to calculate the rankings of each link and update the ranking on database and I need to calculate the rankings every 5 minutes, so I think to use cron jobs. Here are what I have done which may help you.

First we need to understand the crontab commands, it can have different options with this command.

crontab -l  : To display the existing cron jobs created. Before creating cron job, we can use this command to check whether it is already created or not
crontab - r :  Remove user's cron jobs. All cron jobs will be removed.
crontab -e : Edit user's cron job, you can create a new cron job using this command. This command will open the default editor of your system, for me it's vi, if you prefer other editors such as vim. You can use export EDITOR=vim command.
crontab -u : Specify the user of the cron job

Ok, now we start to create a new cron job using crontab -e, this will open an editor, you can type the cron job into the editor. The syntax for the cron job is

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
The first five fields specify the time of the scheduled running job, the * means that for each cycle of the time field (For example, the * on hour means each hour, the * on day means each day, if * appears on both hour and day field, it means every hour of every day). If we want to specify a particular time, we should put a number on relative time field. For example, if we want to run a cron job at 7:30 am every day, we should specify the cron job as

30 7 * * * command to be executed

Now if we want to run a cron job every 5 minutes, what should we do? On some systems we can use the following method:

*/5 * * * * command to be executed

If we want to run a job every 10 minutes

*/10 * * * * command to be executed

All right, we are going to add the commands to be run now. The commands can be any executable scripts, programs or commands. For my case, I need to run a php script, so I need to find the php executable which is used to parse and run my php script. The path for the installed php executable on CentOS is /usr/bin/php, so the command to be executed is /usr/bin/php, we can pass the php file to be processed to this php command, suppose my php file is located at /var/www/html/rank.php., we should put this behind the /usr/bin/php. So the complete cron job is:

*/5 * * * * /usr/bin/php /var/www/html/rank.php

Finished? Maybe not. Since our php script may echo some data or some error messages out, by default, these output will be sent to our admin email. If we don't want to receive the output with our email, instead we want them to be logged into a log file, we can add following command : >/var/www/html/rank.log 2>&1

So the complete command should be :

*/5 * * * * /usr/bin/php /var/www/html/rank.php >/var/www/html/rank.log 2>&1

Now save and quit the editor. The cron job is created, after 5 minutes you will find a log file is created in /var/www/html directory. This means your cron job is running.
Save as PDF Mark as read Mark as important
By clicking the "Mark as read" button, this article will be marked as read. It will be removed from the homepage's latest news and the article list on the "Technical article" page in following visits and it will be put to your read list which you can find in "Amin->Article read list". There you can unmark the read articles.
By clicking the "Mark as important" button, this article will be put to your important article list which you can find in "Amin->Article important list". Later when you want reread this article, it's easier for you to find it by checking the "Article important list".

Tags : cron job,crontab,CentOS,linux,php,example   Read(16730) Comment(0) Version(2)

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious 

 Previous : Haxe: Another multiplatform language
 Next : A serious security vulnerability found in MySQL/MariaDB

  ::Related Articles

  ::Comment Zone  (Total 0 comment)

No comment for this article.

  ::Comment

Nickname  
Email 
Comment

:: Other versions

View all

:: Recent articles

:: Most read

:: Contribute

Want to share with the world your understanding about technology? Want to record the process you solve a technical problem? Want to make the world benefit from your understanding and solution? Write them down. You make the world better, the world makes us better.

Write article

:: Find us

Back to top