linux server – cron , crontab

Cron

Cron is a daemon that executes scheduled commands. Cron is started automatically from /etc/init.d Cron searches its spool area (/var/spool/cron/crontabs) for crontab files. It load crontab files on memory to run.

cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon.

 

We cannot access crontab files directly , it will be accessible via crontab.

Cron also reads /etc/crontab, which is in a slightly different format. Additionally, cron reads the files in /etc/cron.d.

Cron runs every minute and check for whether any job has to run or not. It can modify automatically so we do not nee dto restart server after making changes to crontab.

Crontab

Crontab is the program which contains lists of job to be run. Crom daemon looks that list and run the job as per scheduled.

 

crontab -e    Edit your crontab file, or create one if it doesn’t already exist.
crontab -l      Display your crontab file.
crontab -r      Remove your crontab file.
crontab -v      Display the last time you edited your crontab file. (This option is only available on a few systems.)

 

Crontab format:-

Commands are executed by cron when the minute, hour, and month of year fields match the current time, and when at least one of the two day fields (day of month, or day of week) match the current time.

 

Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

 

A field may be an asterisk (*), which always stands for “first-last”.

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an “hours” entry specifies execution at hours 8, 9, 10 and 11.

if you want to say “every two hours”, just use “*/2”.

example

minutes , hours, day of month , month , day of week, command(or path of shell script)

An example of crontab format with commented fields is as follows:

# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
    0        2          12             *               0,6           /usr/bin/find

reference:

 

http://www.pantz.org/software/cron/croninfo.html

http://www.adminschoice.com/crontab-quick-reference/

example of crontab:-

[anand@server001 ~]$ crontab -l
#*/5 * * * * /home/anand/scripts/apacheMon.sh
*/5 * * * * /home/anand/scripts/mysqldMon.sh
#*/5 * * * * /home/anand/scripts/apacheaccesslog.sh
*/5 * * * * /home/anand/scripts/apacheerrorlog.sh
*/10 * * * * /home/anand/scripts/cpuidle.sh
*/10 * * * * /home/anand/scripts/dbstat.sh
*/5 * * * * /home/anand/scripts/diskalert.sh
00 00 * * * /home/anand/scripts/deadlock.sh

[anand@server001 ~]$ more /home/anand/scripts/deadlock.sh
#!/bin/sh
#***********************************************************************
#Script to check deadlock
#Created by Anand
#***********************************************************************
Maillist=”anand.kumar@test.com”
host=`hostname`
File_Location=”/apps/www/anand/log”
#printf ‘file location’$File_Location
#dateformat=`date +%d-%m-%Y`
dateformat=`date +%d-%m-%Y -d “1 day ago”`
#printf ‘date format’$dateformat
FILE=$File_Location/$dateformat\_DbFailure.txt
if [ -f $FILE ];
then
Count=`grep -i “Invalid query: Deadlock found when trying to get lock;” $FILE | wc -l`
if [ $Count -gt 0 ]
then
echo -e “Invalid query: Deadlock found on Server 001, Please manually run the insert commands.\n Number of deadlock found:$Count.\n Path of insert query is: $FILE” > /tmp/Error
mail -s “Critical!! – Deadlock while insertion in server001” $Maillist < /tmp/Error
fi
fi
[anand@server001 ~]$ vi /home/anand/scripts/deadlock.sh

To edit the shell script.

How to Install SQuirrel SQL Client on windows

                                                  How to install Squirrel SQL Client

STEP 1. Hit the URL:- http://www.squirrelsql.org/#installation

 

Click on the link on the page

Install jars of SQuirreL 3.3.0 for Windows/Linux/others

After clicking the above link , you are able to download “squirrel-sql-3.3.0-install.jar”.

Save “squirrel-sql-3.3.0-install.jar” file in C Drive.

 

STEP 2:- Open Command line (CLI) [start->run->cmd and press ENTER]

C:\Documents and Settings\Kumar>cd ../..

C:\>

Now , type java -jar squirrel-sql-3.3.0-install.jar at CLI and press ENTER

It will open SQuirrel software installation package. Just click on next , next and install the software.

STEP 3:- Open Squirrel software and click on Drivers tab

 

STEP 4:- To enable Oracle Thin Driver , you have to download ojdbc14.jar and add this jar file in “Squirrel\lib” folder.

 

Download link: http://www.findjar.com/jar/mule/dependencies/maven1/oracle-jdbc/jars/ojdbc14.jar.html

 

Restart the Squirrel software, where you can able to see that Oracle Thin Driver is enabled.

 

 

STEP 5:-

Click on Aliases tab , click on +(add) button to add new database.

In ADD Alias , provide NAME for Database ,

Select Oracle Thin Driver  in Driver drop down box.

Provide the URL , Username and password.

URL should be like:-

jdbc:oracle:thin:@101.101.198.134:1621:PRODDB

 

 

Click on TEST button , if all details are correct then it should show below mentioned  message on screen.

CSS behaviour on IE & Other Browsers

In this post , i want to emphasize how exactly the css behave on IE and Other browsers like firefox , google chrome , opera etc.

I would like to share my personal experience and challenges which i have faced during working on different different browser for an application.
here are the some challenges in key points:

1.Display box in IE & other browser

Solution:
For IE:-

#box{ display:inline; border: solid 1px; }

For Other browser:-
#box {
display: inline-block;
border: solid 1px;
}

2.Anchor tag (hyperlink) visited issue

Html code :

tesing hyperlink

a:link {
color: #A5ACB0;
}
/* visited link */
a:visited {
color: #00ADEF;
}
/* selected or active link */
a:active {
color: #A5ACB0;
}
/* Hover link */
a:hover {
color: #00338E;
}

Test IE
Test ALL

For IE Broswer:-
After clicking on TestIE link , it will show a:visited color but when you go to other page and come back to the same page where you have TestIE link , the color of hyperlink will as a:link .
Logically , it should be a:visited color because you have visited the link.
This logic won’t work on IE 6,7,8 and other latest IE versions but work perfectly fine on any other browsers.

But After clicking on TestALL link on any browser, it shows visited color .

Image onmouseout , onmouseover and onclick

Requirement:- A web page should contain an image say top arrow image.
When user visit to a page , top arrow image should be in disable form (say in grey color) , when user hover over the image , it should get activated and turns to other color (say in green) and when user clicks on that image , it should turns to other color (say blue).

images:- top_arrow_disable.jpg , top_arrow_active.jpg and top_arrow_visited.jpg

1st Solution:-

<script type=”text/javascript”>
function checkHidden(id,checkVal){
var hiddenVal = document.getElementById(‘check’).value;
if(hiddenVal==’clicked’){
if(checkVal==’over’){
document.getElementById(id).src=”images/up_arrow2.gif”;
}
if(checkVal==’out’){
document.getElementById(id).src=”images/up_arrow3.gif”;
}
//document.getElementById(id).src=”images/up_arrow3.gif”;
//alert(‘Link is already clicked:Onclick takes precedence’);
}
else{
if(checkVal==’over’){
//alert(‘on mouse over’);
document.getElementById(id).src=”images/up_arrow2.gif”;
}
else if(checkVal==’out’){
//alert(‘on mouse out’);
document.getElementById(id).src=”images/up_arrow1.gif”;
}
else if(checkVal==’clicked’){
//alert(‘just clicked’);
document.getElementById(id).src=”images/up_arrow3.gif”;
document.getElementById(‘check’).value=’clicked’;
}
}
}

</script>
<a  href=”#top” onclick=”checkHidden(‘a1′,’clicked’);” onMouseOver=”checkHidden(‘a1′,’over’);” onMouseOut=”checkHidden(‘a1′,’out’);”>
<img id=”a1″ src=”images/up_arrow1.gif” name=”anandf” width=”16″ height=”16″ border=”0″>
</a>

2nd solution:-

 <script type=”text/JavaScript”>
<!–
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf(“#”)!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
 var p,i,x;  if(!d) d=document; if((p=n.indexOf(“?”))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//–>
</script>

<a href=”#top” onMouseOut=”MM_swapImage(‘Top’,”,’../images/up_arrow3.gif’,1)” onMouseOver=”MM_swapImage(‘Image1′,”,’../images/up_arrow2.gif’,1)”><img src=”../images/up_arrow1.gif” name=”Image1″ width=”16″ height=”16″ border=”0″></a>

PHP PDO

PHP PDO :-PHP Data object is a database connection abstraction library for PHP 5.

What is  PDO ?

  • Its written in complied language(c/c++) , other php libraries are written in Interpreted language (AdoDB , PEAR DB)
  • its lightweight database connection abstraction library

WHY PDO ?

  • It support many databases , so you don’t need to write lines of code of each database , just write one code and run on any database
  • Speed . PDO is written in complied language but others php libraries like PEAR DB ,Adodb are written in interpreted language.

PHP supports many databases like mysql , sqllite , postgre sql etc.

mysql_connect($host,$user,$password)

sqllite_open($host,$user,$password)

pg_connect($host,$user,$password)

so if you have to change your database from say mysql to postgre or viceversa , then you have to change your code , so you have to do rework on that.

thats way PDO cam ein picture , so that you have to write just one code and it will be work on any database platform without changing codes again and again.

PDO: Activation PHP Data Objects Extension

Goto PHP/ext folder and check whether pdo extension exists or not.

php_pdo.dll , php_pdo_mysql.dll etc.

To enable PDO , go to PHP.INI file and uncommnet the code

;extension=php_pdo.dll -> extension=php_pdo.dll

;extension=php_mysql_pdo.dll -> extension=php_mysql_pdo.dll

Restart your apache server.

PDO: connection to databases

//mysql connection

$con = new PDO(‘mysql:host=$host,dbname=$DB’,$user,$pwd);

//sqllite connection

$con = new PDO(‘sqllite:$DB’);

//postgre connection

$con = new PDO(‘pqsql:host=$host,dbname=$DB’,$user,$pwd);

or we can write like this also

<?php
// configuration
$dbtype		= "sqlite";
$dbhost 	= "localhost";
$dbname		= "test";
$dbuser		= "root";
$dbpass		= "admin";
$dbpath		= "c:/test.db";

// switching
switch($dbtype){
  case "mysql":
    $dbconn = "mysql:host=$dbhost;dbname=$dbname";
    break;

  case "sqlite":
    $dbconn = "sqlite:$dbpath";
	break;

  case "postgresql":
    $dbconn = "pgsql:host=$host dbname=$db";
	break;
}

// database connection
$conn = new PDO($dbconn,$user,$pass);

?>

PDO: Fetch Mode

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

// query
$sql = "SELECT title FROM books ORDER BY title";
$q	 = $conn->query($sql);

// fetch
while($r = $q->fetch()){
  print_r($r);
}

// result //Array ( [title] => PHP AJAX [0] => PHP AJAX )


// query
$sql = "SELECT title FROM books ORDER BY title";
$q	 = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC); //fetch association 
// fetch
while($r = $q->fetch()){
  print_r($r);
}

// result //Array ( [title] => PHP AJAX)


$q->setFetchMode(PDO::FETCH_NUM);

// fetch
while($r = $q->fetch()){
  print_r($r);
}

// result //Array ( [0] => PHP AJAX )
$q->setFetchMode(PDO::FETCH_BOTH);
$q	 = $conn->query($sql);

// fetch
while($r = $q->fetch()){
  print_r($r);
}

// result //Array ( [title] => PHP AJAX [0] => PHP AJAX ) 




Drupal termnology

Basically, there are 5 main layers in Drupal where information flows,

  1. Data (Node, ETC)
  2. Modules
  3. Blocks and Menus
  4. User Permissions
  5. Template

 

Data
Base of the system is collection of Nodes. – the data pool

Modules
These are functional plugins that are either part of the Drupal core (they ship with Drupal) or they are contributed items that have been created by members of the Drupal community.

Blocks and Menus
This is the next layer where we find blocks and menus. Blocks often provide the output from a module or can be created to display whatever you want, and then can be placed in various spots in your template (theme) layout. Blocks can be configured to output in various ways, as well as only showing on certain defined pages, or only for certain defined users.

User Permissions
Here is the next layer where settings are configured to determine what different kinds of users are allow to work and see.

Template
This is mainly the site theme or the skin. This is made up predominantly of XHTML and CSS, with some PHP variables intermixed

Understanding this flow of information is very important if you are facing A Drupal project targeted interview as the interviewer might test your overall knowledge  of  Drupal CMS by asking this sort of questions.

 

 

 

 

 

 

What is taxonomy in drupal?

We can define any number of vocabularies (category types) and terms (categories)
e.g.
Controversial Content: (vocabulary)
– violence (term)
– adult content (term)

Genre (Vocabulary)
– Comedy (term)
– Romance (term)

We can config which content types are compatible with which vocabulary and which nodes are belong to which terms.

Some important drupal modules: Views, CCK, Path auto, FCK Editor, User points, Flags, Panels, Image cache

 

What is a block?

Block is a section in a page (recent posts, news, vote to something, login box, navigation), which is movable to regions (header, footer, content top etc) in a page. Blocks can be configured to show/hide only to certain type of user roles, url patterns or some other advanced criteria. (Content area is not a block)

What is a node in Drupal?

A node is a unit of content. It has a type. Page, News or as Testimonial.

Which are the core required modules in drupal 6.x ?

1. Block — Controls the boxes that are displayed around the main content.
2. Filter — Handles the filtering of content in preparation for display.
3. Node — Allows content to be submitted to the site and displayed on pages.
4. System — Handles general site configuration for administrators.
5. User — Manages the user registration and login system.

 

 

 

 

 

Drupal Modules

These key modules are some of the most important ones to add no matter what type of site you want to build with Drupal.

We need  TOKEN module http://drupal.org/project/token

 

Download FCKEDitor from sourcrforge and unzip it.

Place this unzip FCKEditor folder inside sites/aa/modules/FCKEditor .

There will be another folder as FCKEditor in FCKedior module folder ,copy the unzip from above link and copy here .

  • IMCE for uploading images to insert into posts
  • Poor Mans Cron for easy execution of the Drupal cron file

What is VIEW Module ?

VIEW is a powerful query builder for Drupal that allows you to fetch and present lists and tables of content (posts/nodes) to the user in ways that are tailored to your site and your content.

The Views module provides a flexible method for Drupal site designers to control how lists and tables of content (nodes in Views 1, almost anything in Views 2) are presented. Views can be used to generate reports, create summaries, and display collections of images and other content.

 

You need Views if:

  • You like the default front page view, but you find you want to sort it differently.
  • You like the default taxonomy/term view, but you find you want to sort it differently; for example, alphabetically.
  • You use /tracker, but you want to restrict it to posts of a certain type.
  • You like the idea of the ‘article’ module, but it doesn’t display articles the way you like.
  • You want a way to display a block with the 5 most recent posts of some particular type.
  • You want to provide ‘unread forum posts’.
  • You want a monthly archive similar to the typical Movable Type/Wordpress archives that displays a link to the in the form of “Month, YYYY (X)” where X is the number of posts that month, and displays them in a block. The links lead to a simple list of posts for that month.

 

jQuery Tutorial Demo: Select All Checkboxes & validate Form

This tutorial will teach you to add validation using JQUERY validate.js file and get multiple checkbox valu using JQUERY jquery.js file.

These two js files jquery.js , jquery.validate.js is very useful when user wants to do validation of form on client side server .

Here the attached code.just download these two js files from JQUERY site and embedd in below code and run the file on browser .

<html>
<head>
<title>jQuery Tutorial Demo: Select All Checkboxes</title>
<script type=”text/javascript” src=”/jquery/jquery.js”></script>
<script type=”text/javascript” src=”/jquery/jquery.validate.js”></script>
<style type=”text/css”>body{font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px}
h1, h2{font-size:20px;}</style>

<script type=”text/javascript”>
$(document).ready(function()
{
$(“#paradigm_all”).click(function()
{
var checked_status = this.checked;
$(“input[name=paradigm[]]”).each(function()
{
this.checked = checked_status;
});
});
});

//initiate validator on load
$(function() {
// validate contact form on keyup and submit
$(“#userForm”).validate({
//set the rules for the field names
rules: {
first_name: {
required: true,
minlength: 2
},
last_name: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
company: {
required: true,
minlength: 2
},
},
//set messages to appear inline
messages: {
first_name: “*Please enter First Name”,
last_name: “*Please enter Last Name”,
email: “*Please enter a valid Email address”,
company: “*Please enter Company Name”
}
});
});

</script>
<style type=”text/css”>
.error {
color: red;
font: 12pt verdana;
padding-left: 10px
}
</style>
</head>
<body>

<h1>jQuery Tutorial: Multiple Checkbox values & Validation of FORM </h1>
<p>You will learn how to select/deselect all checkbox and get all the values of checked box.<br/>
This will also teach you to validate your FORM using JQUERY validate.js.
</p>
<br/><br/>
<?php
if(isset($_POST) && !empty($_POST))
{
var_dump($_POST);
}
?>
<form name=”myform” action =”#” id=”userForm” method=’post’ enctype=’multipart/form-data’>
<label for=”first_name”>First Name</label><br />
<input name=”first_name” id=”first_name” /><br />
<label for=”last_name”>Last Name</label><br />
<input name=”last_name” id=”last_name” /><br />
<label for=”email”>Email address/Username</label><br />
<input name=”email” id=”email” /><br />
<label for=”company”>Company</label><br />
<input name=”company” id=”company” /><br /><br />
<label>Role</label><br/>
<input type=”checkbox” name=”paradigm[]” value=”Imperative”>Imperative<br/>
<input type=”checkbox” name=”paradigm[]” value=”Object-Oriented”>Object-Oriented<br/>
<input type=”checkbox” name=”paradigm[]” value=”Functional”>Functional<br/>
<input type=”checkbox” id=”paradigm_all”>Select All<br><br />
<input type=”submit”  value=”save” />

</body>
</html>