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/test.sh    ( the simplest entry)
To run any particular time limit of each hour
20 * * * * * /root/test.sh     ( will run at each hour :20 )
You can use comma for different minutes as below
20,30,40,50 * * * * *  /root/test.sh 
To run on each hour 
00 * * * * * /root/test.sh
To run on each min of particular hour
* 1,2,5 * * * /root/test.sh
For each field , below are the values.
FieldRange of values
minute0-59
hour0-23
day1-31
month1-12
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

e.g   
[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