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
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
Running php script on command line & installing php5-cli library in phpserver
To run php in command line, you need to not only PHP, but also the PHP5-cli library in your php server. The PHP5-cli stands for command-line interpreter for the PHP5 scripting language. It is very useful when we need test our scripts from the shell. In some cases we need to install this library for running cronjobs on the server.
how to install php5-cli. in linux machines.
first login as super user : " su username "
after login just use the following command "sudo apt-get install php5-cli "
the php5-cli installation has completed
just restart your apache server. For that we can use the following command
How to restart a apache server on linux
sudo /etc/init.d/apache2 restart
After the restart of your server, the php5-cli has been installed on your server. and just try to run a simple php script on your server.
/*******************************/
# function to get the byte format #
# @phpqa.blogspot.com #
/*******************************/
function getByteFormat($number,$
$unit = "";
switch($number){
case($number >= 1099511627776):
$number = round($number / 1099511627776, 1);
$unit = $caption == "small" ? "TB" : "Terabyte";
break;
case($number >= 1073741824):
$number = round($number / 1073741824, 1);
$unit = $caption == "small" ? "GB" : "Gigabyte";
break;
case($number >= 1048576):
$number = round($number / 1048576, 1);
$unit = $caption == "small" ? "MB" : "Megabyte";
break;
case($num >= 1024):
$number = round($number / 1024, 1);
$unit = $caption == "small" ? "KB" : "Kilobyte";
break;
default:
$unit = $caption == "small" ? "B" : "Byte";
$number = $number;
break;
}
return number_format($number, 2).' '.$unit;
}
# How to use #
echo getByteFormat(10995116277,"
echo getByteFormat(109951162799,"
echo getByteFormat(10995116261099);
?>
please try this function and lemme give your feedback as comments
Popular Posts
- MySQL stored procedure&triggers
- php questions
- How we canmove into last changed position on info window close event On google map
- Pattern Matching in mysql
- MYSQL joins
- Jquery based dynamic adding and removal of html elements.
- securing a form with captcha
- Comparing a date with current date
- Css
- php Questions