| 0 comments ]

Adding cron tasks in Linux using SSH

in most hosting platforms we have the control panel console to add cron tabs. But in the case of our local linux machine, or server's which does not have control panel, we could use the following

adding the commands in the crontab

Login in the console as super user

Use the following command " sudo crontab -e " to open the crontab file.
Some times you will get a message to select the editor to open the file. select the desired editor. the crontab file will open and it contains the syntax for the crontab command. its looks like " # m h dom mon dow command "

m -> minute
h -> hour
dom -> date of month
mon -> Month
dow -> day of week

for setting cron for every minute use the following code

* * * * * yourcommand

for each five minutes

*/5 * * * * yourcommand

For hourly cron

0 * * * * yourcommand

For daily cron

0 0 * * * yourcommand

for yearly

0 0 1 1 * yourcommand


we could also run the cron in particular minute ,hour etc.

15,45 * * * yourcommand

the cron will run each 15 and 45 min of every hour

0 12,24 * * yourcommand

the cron will run each 12 and 24 every hour


where the value of * holds value "Every".


After setting the cronjob save the cron tab file.


For testing these cronsettings please follow following steps..

create the php script and save as mycron.php

root","root");
mysql_select_db("crontest");
$date = date("Y-m-d H:i:s");
mysql_query("INSERT INTO `test`(`timer`) VALUES ('$date')");
?>

create corresponding database and table

CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL auto_increment,
`timer` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM



open crontab file add following command


*/5 * * * * php /pathto/mycron.php

and save the file and check back the mysql table for inserted values.

I hope this post will help you know details about adding cron job without the control panel or plesk console in your server or in the local machine

0 comments

Post a Comment

Please put your comments here. your questions, your suggestions, also what went wrong with me.