Monday, September 23, 2013

Date command with examples

Date command is most essentially used in scripting for date wise backups and in other scenarios.                                                                
                                                                     
                                                                     
                                             
    date +"%Y/%m/%d %H:%M"

is correct and 

    date +%Y/%m/%d %H:%M  

is not.
Please note that if you date conversion template contains spaces you need to put it in single or double quotes

If  you are OK with two year year representation you can use %y instead of %Y. There is also a macro %D that provides date in  mm/dd/yy format, so we can rewrite the previous example as

     date +"%D %H:%M"

Here  are most important build-in macros:

        %% a literal %
        %a locale's abbreviated weekday name (Sun..Sat)
        %A locale's full weekday name, variable length (Sunday..Saturday) %b locale's abbreviated month name (Jan..Dec)
        %B locale's full month name, variable length (January..December)
        %c locale's date and time (Sat Nov 04 12:02:33 EST 1989)
        %d day of month (01..31)
        %D date (mm/dd/yy)
        %e day of month, blank padded ( 1..31)
        %H hour (00..23)
        %j day of year (001..366)
        %m month (01..12)
        %M minute (00..59)
        %n a newline
        %p locale's AM or PM
        %r time, 12-hour (hh:mm:ss [AP]M)
        %S second (00..60)
        %t a horizontal tab
        %T time, 24-hour (hh:mm:ss)
        %U week number of year with Sunday as first day of week (00..53)
        %V week number of year with Monday as first day of week (01..52)
        %w day of week (0..6); 0 represents Sunday
        %W week number of year with Monday as first day of week (00..53)
        %x locale's date representation (mm/dd/yy)
        %X locale's time representation (%H:%M:%S)
        %y last two digits of year (00..99)
        %Y -- ccyy year

Date also can work not with the current time, but with time of modification of the referenced file

    date -r /etc/passwd +"%y%m%d"

It also can serve as data calculator.
Various options for past days (GNU date only):

date +"%Y%m%d" -d sunday # GNU date
20060709

date +"%Y%m%d" -d last-sunday # GNU date
20060702

date +"%Y%m%d" -d last-week # GNU date
date -v -1m +"%Y%m%d" # BSD date
20060627

date +"%Y%m%d" -d last-month # GNU date
date -v -1w +"%Y%m%d" # BSD date
20060604

date +"%Y%m%d" -d last-year # GNU date
date -v -1y +"%Y%m%d" # BSD date
20050704

date +"%Y%m%d" -d next-week # GNU date
date -v 1w +"%Y%m%d" # BSD date
20060711

date +"%Y%m%d" -d next-month # GNU date
date -v 1m +"%Y%m%d" # BSD date
20060804

date +"%Y%m%d" -d next-year # GNU date
date -v 1y +"%Y%m%d" # BSD date
20070704

To show the time in seconds since 1970-01-01 (Unix epoch):

date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
1240596879

To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:

date -d "UTC 1970-01-01 1240596879 secs"
Fri Apr 24 13:14:39 CDT 2009

Or:

date -ud @1000000000
Sun Sep  9 01:46:40 UTC 2001

    Examples

date "+%-m/%-d/%y" 
7/4/06

date "+%Y%m%d"
20060704

    $ date  +"%b %d, %Y"
    Mar 14, 2010

    date -d yesterday +"%Y/%m/%d"
    2010/03/13

Yesterday assigned to variable

DATE=$(date -d yesterday +"%Y%m%d")
echo $DATE
20060704

To assign the time to a variable

START=`date '+%r'`
echo $START
03:06:02 PM
sleep 5
echo $START
03:06:02 PM

To show the time in a different timezone, the TZ environment variable is read, Timezone types is found in /usr/share/zoneinfo

OLDTZ=$TZ
export TZ=GMT; echo "GMT:               `date +\"%F %R (%Z)\"`"
GMT:               2008-10-31 12:30 (GMT)
export TZ=Europe/Stockholm; echo "Stockholm:    `date +\"%F %R (%Z)\"`"
Stockholm:    2008-10-31 13:30 (CET)
export TZ=Asia/Kuala_Lumpur; echo "Kuala Lumpur: `date +\"%F %R (%Z)\"`"
Kuala Lumpur:        2008-10-31 20:30 (MYT)
export TZ=US/Central; echo "Dallas:             `date +\"%F %R (%Z)\"`"
Dallas:             2008-10-31 07:30 (CDT)
export TZ=$OLDTZ

Using date for offset calculation:

date +"%Y%m%d" -d sunday
20060709

date +"%Y%m%d" -d last-sunday
20060702

date +"%Y%m%d" -d last-week
20060627

date +"%Y%m%d" -d last-month
20060604

date +"%Y%m%d" -d last-year
20050704

date +"%Y%m%d" -d next-week
20060711

date +"%Y%m%d" -d next-month
20060804

date +"%Y%m%d" -d next-year
20070704

Tuesday, September 3, 2013

Cron Format in linux.

Cron Format

Cron is a simple and powerful way to perform various actions in a timely manner. It also plays a main role in automating tasks.

cron format consists of five fields separated by white spaces:

<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week>


Cron format:

* * * * *     followed by command to be executed.
| | | | |
| | | | |
| | | | +----   Day of the Week   (range: 1-7, 1 standing for Monday)
| | | +------  Month of the Year (range: 1-12)
| | +-------- Day of the Month  (range: 1-31)

| +---------- Hour              (range: 0-23)
+------------ Minute            (range: 0-59)


Any of these 5 fields may be an asterisk (*). This would mean the entire range of possible values, i.e. each minute, each hour, etc.

Any field may contain a list of values separated by commas, (e.g. 1,3,7) or a range of values (two integers separated by a hyphen, e.g. 1-5).

After the asterisk or values, you can use character / to do repeated action with certain intervals.

For example, you can write "0-23/2" in Hour field to specify that some action should be performed every two hours (it will have the same effect as "0,2,4,6,8,10,12,14,16,18,20,22"); value "*/4" in Minute field means that the action should be performed every 4 minutes, "1-30/3" means the same as "1,4,7,10,13,16,19,22,25,28".

In Month and Day of Week fields, you can use names of months or days of weeks abbreviated to first three letters ("Jan,Feb,...,Dec" or "Mon,Tue,...,Sun") instead of their numeric values.

Examples:

* * * * *                              Each minute


59 23 31 12 5                      One minute  before the end of year if the last day of the year is Friday.

(or)

59 23 31 DEC Fri                Same as above


00 12 13 7 *                        Every  year, on July 13th at 12:00


0,15,30,45 0,6,12,18 1,15,31 * 1-5  At 00:00, 00:15, 00:30, 00:45, 06:00, 06:15, 06:30,06:45, 12:00,                                                                12:15, 12:30, 12:45, 18:00, 18:15,18:30, 18:45, on 1st, 15th or  31st                                                          of each  month, but not on weekends


*/15 */6 1,15,31 * 1-5                    Same as above


0 12 * * 1-5 *                             At midday on weekdays


* * * 1,3,5,7,9,11 *                        Each minute in January,  March,  May, July, September, and November


1,2,3,5,20-25,30-35,59 23 31 12 *  On the  last day of year, at 23:01, 23:02, 23:03, 23:05,
                                                         23:20, 23:21, 23:22, 23:23, 23:24, 23:25, 23:30,
                                                          23:31, 23:32, 23:33, 23:34, 23:35, 23:59


0 9 1-7 * 1                                    First Monday of each month, at 9 a.m.



0 0 1 * *                                      At midnight, on the first day of each month


* 0-11 * * *                                Each minute before midday


* * * 1,2,3 *                               Each minute in January, February or March



* * * Jan,Feb,Mar *                      Same as above


0 0 * * *                                    Daily at midnight


0 0 * * 3                                     Each Wednesday at midnight


You can also use special strings like below


@reboot   At startup.

@yearly   Run once a year

0 0 1 1 *   (Same as above)

@annually (same as @yearly)

@monthly   Run once a month

0 0 1 * *     (same as above)

@weekly  Run once a week

0 0 * * 0     (same as above)

@daily         Run once a day

0 0 * * *      (same as above)

@midnight    (same as @daily)

@hourly         Run once an hour

0 * * * *        (same as above)


you can use

crontab -e to edit and add your cronjobs.

crontab -l to list the cronjobs.

Before pressing the enter button , ensure that you haven't entered crontab -r as it would delete all the cronjobs.

To check the users cronjobs, use the below syntax.

crontab -u username -l

Install postgresql in linux

Download postgresql software

wget http://ftp.postgresql.org/pub/source/v9.2.3/postgresql-9.2.3.tar.bz2                            
tar -jxvf postgresql-9.2.3.tar.bz2                                                                  
cd postgresql-9.2.3    
       
./configure --without-readline
 make
 make install

[root@machine1 postgresql-9.2.3]# useradd postgres                                                          
[root@machine1 postgresql-9.2.3]# mkdir /usr/local/
bin/     etc/     games/   include/ lib/     lib64/   libexec/ pgsql/   sbin/    share/   src/  
[root@machine1 postgresql-9.2.3]# mkdir /usr/local/pgsql/data
[root@machine1 postgresql-9.2.3]# chown postgres /usr/local/pgsql/data
[root@machine1 postgresql-9.2.3]# /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
initdb: cannot be run as root                                                      
Please log in (using, e.g., "su") as the (unprivileged) user that will              
own the server process.                                                            
[root@machine1 postgresql-9.2.3]# su - postgres
[postgres@machine1 ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.                                

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".  

fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok                                    
selecting default max_connections ... 100                          
selecting default shared_buffers ... 32MB                          
creating configuration files ... ok                                
creating template1 database in /usr/local/pgsql/data/base/1 ... ok
initializing pg_authid ... ok                                      
initializing dependencies ... ok                                  
creating system views ... ok                                      
loading system objects' descriptions ... ok                        
creating collations ... ok                                        
creating conversions ... ok                                        
creating dictionaries ... ok                                      
setting privileges on built-in objects ... ok                      
creating information schema ... ok                                
loading PL/pgSQL server-side language ... ok                      
vacuuming database template1 ... ok                                
copying template1 to template0 ... ok                              
copying template1 to postgres ... ok                              

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
or
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

[postgres@machine1 ~]$ /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &
[1] 26097
[postgres@machine1 ~]$ /usr/local/pgsql/bin/createdb test
/usr/local/pgsql/bin/createdb: symbol lookup error: /usr/local/pgsql/bin/createdb: undefined symbol: PQconnectdbParams
[postgres@machine1 ~]$  LD_LIBRARY_PATH=/usr/local/pgsql/lib
[postgres@machine1 ~]$ export  LD_LIBRARY_PATH=/usr/local/pgsql/lib
[postgres@machine1 ~]$ /usr/local/pgsql/bin/createdb test
[postgres@machine1 ~]$

create database

[postgres@machine1 ~]$ /usr/local/pgsql/bin/psql test
psql (9.2.3)
Type "help" for help.

test=#
test=#
test=#

change postgres password

[postgres@machine1 ~]$ /usr/local/pgsql/bin/psql test
psql (9.2.3)
Type "help" for help.

test=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'postgres';
ALTER ROLE
test=# \q

Edit the postgre configuration file and add the below lines

[root@machine1 postgresql-9.2.3]# vi /usr/local/pgsql/data/pg_hba.conf

#host    all             all             127.0.0.1/32            trust
to
host    all             all             127.0.0.1/32            md5


vi /usr/local/pgsql/data/postgresql.conf

# - Connection Settings -

#listen_addresses = 'localhost'         # what IP address(es) to listen on;
to
listen_addresses = '*'                  # what IP address(es) to listen on;

uncomment
password_encryption = on



The first time you run "service postgresql start", it asks you to initialize a database, run:

service postgresql initdb


To start the postgres service while starting the server, follow the below steps

[root@machine1 ~]# cp -rf /usr/local/src/postgresql-9.2.3/contrib/start-scripts/linux /etc/init.d/postgres
[root@machine1 ~]# chmod 755 /etc/init.d/postgres
[root@machine1 ~]# /etc/init.d/postgres start
Starting PostgreSQL: ok
[root@machine1 ~]# /etc/init.d/postgres restart
Restarting PostgreSQL: ok


To have PostgreSQL start automatically when the computer boots add
symbolic links from the correct /etc/rc*.d/ directories to
/etc/init.d/postgres. If the normal runlevel is 3 then you really only
need to add it to rc3.d:

[root@machine1 ~]# ln -s /etc/init.d/postgres /etc/rc3.d/S85postgres
[root@machine1 ~]# ll /etc/rc3.d/S85postgres
lrwxrwxrwx 1 root root 20 Jun 28 16:25 /etc/rc3.d/S85postgres -> /etc/init.d/postgres

We now successfully installed the postgresql, if you would like to change the port number, do the changes in  the below configuration file

/usr/local/pgsql/data/postgresql.conf