This post taught you how to install PostgreSQL Database on Linux using source code. That approach is good when number of servers is less. But what if it is to be installed on a large number of servers? 😉 Such as in a clustered environment? Here’s how to achieve it using a shell script. 🙂
Better enable password-less SSH from your master server/jumpbox to all destination servers to avoid typing passwords. Then copy the installer tarball from jumpbox to all servers using below command.
for hst in `cat /home/shashank/hosts.txt`; do scp $hst:/home/shashank; done
Now that installer has been copied to destination servers, its time to run the script from jumpbox to install it on all destination servers in one go 🙂 Issue below command to execute automation script & install PostgreSQL on all our target servers.
for hst in `cat /home/shashank/hosts.txt`; do ssh -t $hst 'bash -s' < /home/shashank/postgre_installer.sh; done
Below is the installer script.
#PostgreSQL Automation Installer Script version 1
#Author - Shashank Srivastava
echo "Checking availability of pre-required packages"
sudo rpm -q readline-devel.x86_64
if [ $? != 0 ]
echo "Readline package not found. Installing it...."
sudo yum install readline-devel.x86_64 -y
sudo rpm -q zlib-devel-1.2.3-29.el6.x86_64
if [ $? != 0 ]
echo "zlib package not found. Installing it...."
sudo yum install zlib-devel.x86_64 -y
echo "Unpacking PostgreSQL tarball..."
sudo tar -zxf /home/shashank/postgresql-9.3.5.tar.gz
echo "Tarball unpacked. Creating destination directory for Postgre...."
sudo mkdir -p /opt/PostgreSQL/9.3
echo "Destination directory created. Building installer from source coude...."
sudo ./configure --prefix=/opt/PostgreSQL/9.3
echo "Installer built. Installing it now...."
sudo make install
id -a postgres
if [ $? != 0 ]
echo "postgres user not found. Adding it...."
sudo useradd postgres
echo "postgres user already exists. Proceeding to next step...."
echo "Configuring PostgreSQL...."
sudo mkdir -p /opt/PostgreSQL/9.3/data
sudo chown postgres /opt/PostgreSQL/9.3/data
echo "Defining data directory for Postgre & starting it...."
sudo su - postgres -c "/opt/PostgreSQL/9.3/bin/initdb /opt/PostgreSQL/9.3/data/; sleep 2s; /opt/PostgreSQL/9.3/bin/pg_ctl -D /opt/PostgreSQL/9.3/data/ -l logfile start"
echo "Checking version...."
echo "Script executed successfully!"
My work involves installing & managing a lot of Linux servers. At times I have to do same work on numerous servers. One such task is installing Tomcat. In these posts I showed you how to install Apache Tomcat & how to check its version on Linux servers.
In this post, I will show you how you can automate the installation process across any number of servers using a shell script 🙂
My approach is to : –
- Setup password-less SSH on all servers from one jumpbox machine. Jumpbox is a server from which you will execute this script.
- Create a list of servers in a text file. All servers are typed line by line.
- Copy Java & Tomcat installers to all servers in one go from this jumpbox.
- And finally execute the script.
Below are the commands & script through which I accomplished this. All of these are self-explanatory 😉
for hst in `cat /home/shashank/hosts.txt`; do scp ~shashank/jdk-8u25-linux-x64.tar.gz $hst:/home/shashank; done
for hst in `cat /home/shashank/hosts.txt`; do scp ~shashank/apache-tomcat-8.0.15.tar.gz $hst:/home/shashank; done
What it does above? For loop reads the contents of text file (server IPs) line by line & copies tarballs there.
for hst in `cat /home/shashank/hosts.txt`; do ssh $hst -t 'bash -s' < ~shashank/tomcat_installer.sh; done
The trick above is to use ‘bash -s’. It will accept the script as an argument & execute it. After execution, it will move to next server. Below is my installer script.
Do let me know if it was helpful or not 🙂 I will come up with some more posts soon 🙂
#Apache Tomcat Installer Script
#Author : Shashank Srivastava
echo "Logged into `hostname`. Installing here."
#Checking if installer tarballs are present or not. If they are not found, script will print error message & quit.
if [ -f /home/shashank/jdk-8u25-linux-x64.tar.gz ] && [ -f /home/shashank/apache-tomcat-8.0.15.tar.gz ]
echo "Unpacking Java installer tarball.......";
#waiting for 2 seconds to show you what is being done.
sudo tar -xzvf /home/shashank/jdk-8u25-linux-x64.tar.gz;
echo "Java tarball unpacked.";
echo "Unpacking Tomcat installer tarball.......";
sudo tar -xzvf /home/shashank/apache-tomcat-8.0.15.tar.gz;
echo "Tomcat tarball unpacked.";
echo "Installing Java & Tomcat to /opt/app directory....";
sudo cp -rp /home/shashank/jdk1.8.0_25 /opt/app;
sudo cp -rp /home/shashank/apache-tomcat-8.0.15 /opt/app;
echo "Exporting necessary variables......";
echo "Variables exported.";
echo "Checking Java & Tomcat versions.";
echo "Java is installed at `which java` Directory";
java -cp $CATALINA_HOME/lib/catalina.jar org.apache.catalina.util.ServerInfo;
echo "Starting Tomcat server"
echo "Removing unpacked tarballs from PWD.";
sudo rm -rf /home/shashank/jdk1.8.0_25;
sudo rm -rf /home/shashank/apache-tomcat-8.0.15;
echo "Installer tarballs not found in /home/shashank. Please make sure they exist there. Exiting installation process now."
If you have ever come across a text file that has some unnecessary characters added before file/directory paths & you need to get rid of that, how would you do it? E.g.
Easy way, just edit the file and that’s it 😀
Good one 🙂 But what if the file has hundreds of lines? 😉
So, below is how to do it inside Vi editor 🙂 Open the file & issue this –
Mission accomplished 😀
Since there was ./ in path, we escaped that using \ Then there were two // so we escaped them using \/\/ And since we had to replace it with a /, we added another /\/
Syntax of replacing text in Vi is –
If you have tried running Oracle sqlplus (of course through a script) in a cron job, then you might have encountered this error: –
sqlplus: error while loading shared libraries: libsqlplus.so: cannot open shared object file: No such file or directory
What happens is that, despite defining all the environment variables in /etc/profile or bash_profile, cron doesn’t recognise that & throws above error. Close inspection after debugging the script took me to a conclusion that sqlplus is not able to connect to database, because its not finding its shared library.
If you have encountered this issue, here is how to fix it? 😉
Make sure you have exported required variables. Below are my variables in my .bash_profile. Change yours accordingly.
Make sure you source your .bash_profile before executing the script in cron. Edit your script & add below line at the top.
Now try executing the script in cron. It will be successful 😉 cron will first source the bash_profile, initialise all the variables & then execute the script 🙂
In my last post, I explained how to install Tomcat on Linux. At times, you will need to know what version of Tomcat is installed. So below is how to find it out 🙂
Just go the directory where Apache Tomcat is installed & issue below command.
java -cp lib/catalina.jar org.apache.catalina.util.ServerInfo
It will return info like this : –
Server version: Apache Tomcat/8.0.23
Server built: May 19 2015 14:58:38 UTC
Server number: 18.104.22.168
OS Name: Linux
OS Version: 2.6.32-504.el6.x86_64
JVM Version: 1.8.0_25-b17
JVM Vendor: Oracle Corporation
Apache Tomcat is an Open-Source web server meant to run Java code. Just like Apache serves websites written, e.g, PHP, Tomcat serves websites written in Java. Installing Tomcat on Linux is a whole new experience, compared to if you have done it on Windows 😉 Lets begin this tutorial 🙂
Install Java. Apache Tomcat requires Java to be installed first. So, first install it from its official website.
Download Tomcat. Download Apache Tomcat from its official website. I have installed version 8.0.23. You may choose any compatible version. P.S. This tarball contains all the binaries & software to run Tomcat.
Unpack the Tomcat tarball. Once the tarball has been downloaded, just unpack it to any location where you want it to be installed using command : –
tar -xf apache-tomcat-8.0.23.tar.gz
Start Tomcat. Move to the Tomcat directory & run below script.
To stop Tomcat, just issue –
So, that’s it! Apache Tomcat has been installed & running fine. If you thought you will need to click next..next..continue..etc, then you’re wrong 😉 You just need to place the archive(tarball) in the right location & it is installed!! Installation on Linux is far different 😉
Hope you found this post useful 🙂 Do let me know how was it.
In the previous post, I wrote about how to install PostgreSQL. In this post, I will describe how we can check PostgreSQL version. Issue the below commands for client (psql) & server (postgres) versions.
[root@server shashank]# /opt/PostgreSQL/9.3/bin/psql --version
psql (PostgreSQL) 9.3.5
[root@server shashank]# /opt/PostgreSQL/9.3/bin/postgres --version
postgres (PostgreSQL) 9.3.5