Thursday, 10 March 2016

How to create crontab ( cronjobs) entries

Cron is a job scheduling subsystem for linux. . If you wish to schedule  any job, script or command for any particular time , then crontab is very useful.

Each user on machine can have their own entries while you should keep in mind which command or job you are running should be accessible to that user. Otherwise permission denied error will  be shown.

Some basic uses of cron.:-

crontab -l : It will print out the all cronjobs you have scheduled for that user.

crontab -e: This command is used to install / create new cronjobs for that user.
crontab -u username -l : You should be root user to run this command. It will list all cron of that user you asked.
crontab -r: This command is used to remove all cron entries. 
crontab -u username -r : Will remove all entries of that user.

Note: Keep in mind ,  crontab -r will remove all entries. So please run carefully.

crontab -l > filename.text : It will have all cron entries in text format  on given location. You can also have other extensions too if required.

crontab -l < filename.text: It will restore all entries of cron jobs .

Syntax for crons:-

* * * * * 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)

uses of operators:-

Below are the operators which are very short cuts. There are three operators:
  1. The asterisk (*:  It specifies all values for that in which field ( min, hour, day, etc) it will be defined.  
  2. The comma (,) : This operator specifies a list of values, for example: “1,5,10,15,20, 25”.
  3. The dash (-) : This operator specifies a range of values, for example: “5-15” days , which is equivalent to typing “5,6,7,8,9,….,13,14,15” using the comma operator.
  4. The separator (/) : This operator specifies a step value, for example: “0-23/” can be used in the hours field to specify command execution every other hour. Steps are also permitted after an asterisk, so if you want to say every two hours, just use */2.

Examples & uses of all operators :-

to run a file in each min.
* * * * * /root/    ( the simplest entry)
To run any particular time limit of each hour
20 * * * * * /root/     ( will run at each hour :20 )
You can use comma for different minutes as below
20,30,40,50 * * * * *  /root/ 
To run on each hour 
00 * * * * * /root/
To run on each min of particular hour
* 1,2,5 * * * /root/
For each field , below are the values.
FieldRange of values
day-of-week0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc)

Thursday, 4 February 2016

"Touch" command in linux

The touch is a very useful command in linux , which is used to create, change and modify time stamps of a file.   
Uses of Touch command :-
Several options which are very useful in touch command.
      -c :- if the file does not exist, do not create it
      -r :- Change time stamp of file1 similar to file2
      -a :- change the access time only
      -m :- change the modification time only
      -d :- update the access and modification times
      -t :- creates a file using a specified time
      -B :-  Going back the specified number of seconds
      -F:-  going forward the specified number of seconds.

use 1: Creating new file/files

You can create a single or multiple files in just once . Like touch a or touch a b c etc.
If we simply run touch command without any parameter, then it will create a new file with named that file.
For example. touch file1 , file 2, file 3, then files will be created as below

[root@localhost virender]$ touch file1 file2 file3
[root@localhost  virender]$ ls -ltrh|tail -n 3
-rw-rw-r--  1 root    root    0 Feb  4 12:12 file3
-rw-rw-r--  1 root    root    0 Feb  4 12:12 file2
-rw-rw-r--  1 root   root    0 Feb  4 12:12 file1

If you simply use touch command with file name , then there are two cases as below.
 a. If file is not available in that directory, then a new empty file will be created.
 b. If file is available, then only current date will be changed of already existing file. There will be no effect on size of that file

[root@localhost virender]$ ls -ltrh|tail -n 3
-rw-rw-r--  1 root root    0 Feb  4 12:12 file3
-rw-rw-r--  1 root root    0 Feb  4 12:12 file2
-rw-rw-r--  1 root root   18 Feb  4 12:16 file1
If file is not available in directory & you also don't want to create a new file, then use below
[root@localhost virender]$ touch -c file4    ( no file will be created)
Changing last modified time to current time [root@localhost virender]$ touch file1 
[root@localhost virender]$ ls -ltrh|tail -n 3
-rw-rw-r--  1 root root    0 Feb  4 12:12 file3
-rw-rw-r--  1 root root    0 Feb  4 12:12 file2
-rw-rw-r--  1 root root   18 Feb  4 12:20 file1      ( last modification time changed)

Use 2:  Changing time of file1 as same to file 2
 Use touch -r file1 file2. This command will change time of file2 same of file1. E.g
[root@localhost virender]$ ls -ltrh file*
-rw-rw-r-- 1 root root  0 Jan 29 10:52 file2
-rw-rw-r-- 1 root root  0 Feb  4 12:20 file3
-rw-rw-r-- 1 root root 18 Feb  4 12:20 file1

[root@localhost virender]$
[root@localhost virender]$ touch -r file1 file2
[root@localhost virender]$ ls -ltrh file*
-rw-rw-r-- 1 root root  0 Feb  4 12:20 file3
-rw-rw-r-- 1 root root  0 Feb  4 12:20 file2
-rw-rw-r-- 1 root root 18 Feb  4 12:20 file1

3. Changing access & modification time of file/files to current time.

You can check access & modification time of a file using stat command. E.g

[root@localhost virender]$ stat file1
  File: `file1'
  Size: 18              Blocks: 16         IO Block: 4096   regular file
Device: 806h/2054d      Inode: 3407892     Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  500/ root)   Gid: (  500/ root)
Access: 2016-02-04 12:37:19.000000000 +0530
Modify: 2016-02-04 12:20:00.000000000 +0530
Change: 2016-02-04 12:37:19.000000000 +0530
[root@localhost virender]$
Now you can change access & modification time using options of touch command.
   a.  touch -a file1                      ( will change access time of file1)
   b.  touch -m file1                    ( will change modification time of file1)
   c.  touch -am file1                    ( will change access & modification time of file1)

4. Changing or creating a new file to other time.

Changing time of existing file . use touch -t YYMMDDHHMM.SS  file1
output will be as 
[root@localhost virender]$ touch -t 1510051205.12 file1
[root@localhost virender]$ ls -ltrh file1
-rw-rw-r-- 1 root root 18 Oct  5 12:05 file1
[root@localhost virender]$ stat file1
  File: `file1'
  Size: 18              Blocks: 16         IO Block: 4096   regular file
Device: 806h/2054d      Inode: 3407892     Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  500/ root)   Gid: (  500/ root)
Access: 2015-10-05 12:05:12.000000000 +0530
Modify: 2015-10-05 12:05:12.000000000 +0530
Change: 2016-02-04 13:09:12.000000000 +0530
Creating a new file with specific time stamp
[root@localhost virender]$ touch -c -t 1510051205.12 file5
[root@localhost virender]$ ls -ltrh file*

[root@localhost virender]$ touch -t 1510051205.12 file5
[root@localhost virender]$ ls -ltrh file*
-rw-rw-r-- 1 root root  0 Oct  5 12:05 file5

Monday, 20 January 2014

send form data in email javascript or php

In web development it is very normal that we create 'Contact-Us' Form . And we want to send complete form submitted data in a mail to somebody (normally to ourself ).
There are many ways to do this .
1) using javascript
2) using any server side scripting like PHP .

To understand this first of all, we need to understand concept of sendmail utilites and mail server . for sending mail , we need to have a mail server or mail client . best example for mail client is MS-outlook and mail client is "mail" command in linux .
First way -- javascript  -->
As we know javascript is client side scripting , so it need a client to send mail i.e. mail client .
Example :-
lets take a simple form .
<form action="">
Name <input type="text" name="name" />
Phone <input type="text" name="phone" /> 
<input type="submit" name="submit" value="submit"/>

This is simplest example . If we try to see what happens here , we are using javascript "mailto" function . "mailto" will trigger mail client to send mail that means , it will call you MS-Outlook (if installed and configured ) .This way is not used widely as it is client dependent way and we don't have any control here .

Second way -- server side scripting e.g. PHP -->
php will need a mail server configured to send a mail . As we are developers we can control and configure mail server by our own .
Example :-
<form action="abc.php" method="post">
Name <input type="text" name="name" />
Phone <input type="text" name="phone" /> 
<input type="submit" name="submit" value="submit"/>

$name = $_POST['name'];
$phone = $_POST['phone']; 
$to = "";
$subject = "Contact details ";
$message = "The name is  $name \n\r Contact number is $phone ";



Now this is stable and trusted code to send mails . And everyone should use this way of sending mail .

There are two ways of submitting form i.e. GET and POST . Both methods have their importances .
If we submit form using get method and then all data submitted will be append in the action page url . And we know that url have its limits so we can send only small amount of data using GET method .
While in POST method data is sent in the request body so there is no limit here to send data .
Also in GET method data is visible in th url , so it is not safe . Whenever we need to create safe application we should use POST method .
GET method is fast as everything is simply appended in the url . But in using POST method every request need to maintain data in the body . So it is lsightly time consuming method .

Both methods are useful . So we need to choose wisely according to our application taht which method we should use .

Thank you . Please ask in comments if need any clarification . Suggestions are also welcome .

Thursday, 19 December 2013

php strtotime and mktime time functions

We have discussed about php's date and time functions . for refrence please see HERE
Now we are discussing two more functions . strtotime() and mktime() .


              This function will convert a string to timestamp format i.e. total number of seconds since 1st jan 1971 .
its usages are like :-

$var1 = strtotime("now");
$var2 = strtotime("10 September 2000");
$var3 = strtotime("+1 day");
$var4 = strtotime("+1 week");
$var5 = strtotime("+1 week 2 days 4 hours 2 seconds");
$var6 = strtotime("next Thursday");
$var7 = strtotime("last Monday");

echo date("Y-m-d H:i:s",$var1) . "<br/>";
echo date("Y-m-d H:i:s",$var2) . "<br/>";
echo date("Y-m-d H:i:s",$var3) . "<br/>";
echo date("Y-m-d H:i:s",$var4) . "<br/>";
echo date("Y-m-d H:i:s",$var5) . "<br/>";
echo date("Y-m-d H:i:s",$var6) . "<br/>";
echo date("Y-m-d H:i:s",$var7) . "<br/>";

explanation of above mentioned code is -
all $var1-7 variable will get timestamp according to string passed to the function  .
'now' will give current timestamp
'+1 day' will give tomorrow's timestamp i.e. +24 hours
and so on ..

also we can find the difference between two dates .
example : suppose we need to find the difference in days between '10 december 2012' and '12 july 1947' .  then we will write that program like this
$time1 = strtotime( "10 december 2012");
$time2 = strtotime( "12 july 1947");
$diff = $time1-$time2 ;
$no_of_days = $diff/86400 ;          // 1 day = 86400 seconds .

mktime :-

             This functions also create timestamp , but it take values as different parameters instead of one complete timed string .following is the syntax for mktime() function
mktime(hour,minutes,seconds,month,date,year,is_dst) ;
all parameters are optional .If we pass each parameter then it will show you the results. Just give a small try to the programs and will understand quickly , what it means . for example -
echo date("M-d-Y",mktime(0,0,0,12,36,2012)) . "<br>";
echo date("M-d-Y",mktime(0,0,0,14,1,2010)) . "<br>";
echo date("M-d-Y",mktime(0,0,0,1,1,1991)) . "<br>";
echo date("M-d-Y",mktime(0,0,0,1,1,99)) . "<br>";

Hope this post will help someone . Please ask me in comments if need any more help .