Tuesday, October 23, 2018

A bash script to run a jar file as a backend service in Linux boxes with user ownership

#!/bin/sh
PROCESS_NAME=pname
PATH_TO_JAR=/var/www/html/stable_build/build.jar
LOG_PATH=/home/user/tomcat/wolf/wolf-backend.log
USER=user
pidfile=${PIDFILE-/var/run/${PROCESS_NAME}.pid};
lockfile=${LOCKFILE-/var/lock/subsys/${PROCESS_NAME}};

RETVAL=0

. /etc/init.d/functions

. /home/user/.bashrc

start() {

PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`
if [[ (-n ${PID}) && ($PID -gt 0) ]]; then
echo "${PROCESS_NAME}(pid ${PID}) is already running."
exit;
fi

echo -n $"Starting $PROCESS_NAME ..."
daemon --user $USER java -Dprocessname=${PROCESS_NAME} -jar $PATH_TO_JAR >> $LOG_PATH 2> $LOG_PATH < /dev/null &
sleep 10
PID=`ps -eaf|grep processname=${PROCESS_NAME}|grep -v grep|awk '{print $2}'| tail -1`
RETVAL=$?
[ $RETVAL = 0 ] && touch ${lockfile}
[ $RETVAL = 0 ] && echo "${PID}" > ${pidfile}
echo " Started"
}

status(){
PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`
if [[ (-n ${PID}) && ($PID -gt 0) ]]; then
echo "${PROCESS_NAME}(pid ${PID}) is running."
else
echo "${PROCESS_NAME} is stopped."
fi
}

stop() {
echo -n $"Stopping $PROCESS_NAME: ..."
PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`
if [[ (${PID} -gt 0) ]];
then
kill -9 $PID
RETVAL=$?
[ $RETVAL = 0 ] && rm -f ${lockfile}
[ $RETVAL = 0 ] && rm -f ${pidfile}
fi
echo " Stopped"
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: service $SERVICE_NAME {start|stop|status|restart}"
exit 1
;;
esac

A bash script to install a package w.r.to the OS flavor in Linux

#!/bin/bash

func1()
{
yum -y install httpd
systemctl start httpd
systemctl enable httpd
}

func2()
{
apt-get -y install nginx
/etc/init.d/nginx start
update-rc.d nginx defaults
 }

file1="/etc/redhat-release"
file2="/etc/lsb-release"

if [ -f "$file1" ]
then
echo "Centos box"
cat /etc/redhat-release | grep -i 'Centos' | awk '{if(NR==2) print $0}'; echo $?
func1
elif [ -f "$file2" ]
then echo " Ubuntu Box "
cat /etc/lsb-release | grep -i 'Ubuntu' | awk '{if(NR==2) print $0}'; echo $?
func2
else
echo "desired OS flavor not found"
fi

A simple python script to fetch and save memory stats to sqlite3 database in Linux machines.

#!/usr/local/bin/python
import distro
import os
import time
import sqlite3
import re
memfile = "/home/sibin/python-projects/memstat"
db = sqlite3.connect('/home/sibin/python-projects/data.db')
os_version = distro.linux_distribution()
def mem_info():
  with open("/proc/meminfo", "r") as f:
    lines = f.readlines()
    line1 = lines[0].strip()
    line2 = lines[1].strip()
    line3 = lines[3].strip()
    line4 = lines[14].strip()
    line5 = lines[15].strip()
    line = ("%s \n %s \n %s \n %s \n %s \n %s \n" % (os_version,line1,line2,line3,line4,line5))
    result1 = re.sub('[^0-9]','',line1)
    result2 = re.sub('[^0-9]','',line2)
    result3 = re.sub('[^0-9]','',line3)
    result4 = re.sub('[^0-9]','',line4)
    result5 = re.sub('[^0-9]','',line5)
    cursor = db.cursor()
    cursor.execute('''CREATE TABLE IF NOT EXISTS memoryinfo(id INTEGER PRIMARY KEY,MemTotal TEXT, MemFree TEXT,
    Buffers TEXT, SwapTotal TEXT,SwapFree TEXT)''')
    for i in line:
      cursor.execute('''INSERT INTO memoryinfo(MemTotal, MemFree, Buffers, SwapTotal, SwapFree)
      VALUES(?,?,?,?,?)''', (result1, result2, result3, result4, result5))
      db.commit()
  return mem_info
while True:
  mem_info()
  time.sleep(3)