Thursday, August 9, 2012

How to execute a program at regular intervals in Ubuntu

Automatically executing a program at regular intervals in Ubuntu is undertaken using the Linux's/Unix's cron command. Here are the steps:

Step 1: Create the crontab file using an editor. For example:

# joe mycrontabfile.txt

Step 2: Enter the instructions for cron to follow. For example:

0 14 15,28 * * root /etc/firewall/firewall.sh

The instruction format is:

minute hour day month weekday user command

The values for minute is 0-59, hour is 0-23, day is 0-31, month is 0-12, weekday is 0-11. The asterisk character (*) is a wildcard and a comma (,) is a separator.

The above example means execute the program firewall.sh in the directory /etc/firewall with user root at 2pm on the 15th and 28th day of the month.


Step 3: Load the crontab file using the crontab command:

# crontab mycrontabfile.txt

Autoexecute a program in Ubuntu at Boot

Assuming you have a script file named "firewall.sh", to automatically execute the file at boot time in Ubuntu, issue the following commands:

# mv firewall.sh /etc/init.d/
# chmod +x /etc/init.d/firewall.sh
# update-rc.d /etc/init.d/firewall.sh defaults


To remove the autoexecute command, type:

# update-rc.d -f /etc/init.d/firewall.sh remove


Friday, April 27, 2012

PostgreSQL Replication

The following setup were tested with PostgreSQL 9.1.1.  As far as I know, the same setup can also be achieved starting from version 9.0.  Version 9 added built-in replication to the PostgreSQL database.  In the past PostgreSQL replication can be achieved by using third party software (e.g., Bucardo).  Here are the steps:


On the master server assuming an IP address of 192.168.1.107:
Step 1
Edit the postgresql.conf file as follows:

listen_addresses = 'localhost, 192.168.1.107'
wal_level=hot_standby
max_wal_senders=1
wal_keep_segments=25


Step 2
Assuming the slave server IP address of 192.168.1.104, edit the pg_hba.conf as follows:

host replication all 192.168.1.104/32 trust 


Step 3
On psql CLI (as postgres user), issue the command:

SELECT pg_start_backup('backup');


Step 4
Copy the data directory of the master server and place it on the slave server.   Use the command on psql CLI (as postgres user) to determine the physical location of the directory:

SHOW data_directory;



On the Slave Server assuming an IP address of 192.168.1.104: 

Step 5
Edit the postgresql.conf as follows:

hot_standby=on

Step 6
Create recovery.conf file as follows:

standby_mode='on'
primary_conninfo='host=192.168.1.107'

Step 7
Delete the postmaster.pid file

# rm postmaster.pid


On the Master Server:

Step 8
Issue the command on psql CLI as follows:

SELECT pg_stop_backup();

Simple ignore any warning messages.


Step 9
Restart PostgreSQL on the master server.


On the Slave Server:

Step 10
Start the PostgreSQL server.


That's it.  One last final note.  Test your setup by issuing an INSERT or CREATE TABLE command on the master server.  Check if the data are reflected on the slave server.  If it is reflected, then you have just done database replication on PostgreSQL.