Creating a Linux Service

Linux OS Topics
Post Reply
User avatar
nwclasantha
Posts: 75
Joined: Wed Apr 24, 2013 12:57 am
Location: Malabe

Creating a Linux Service

Post by nwclasantha » Tue May 05, 2015 1:51 pm

Code: Select all

#!/bin/bash
# chkconfig: 345 90 10
# processname: tomcat_all
. /etc/rc.d/init.d/functions

LOCKFILE=/var/lock/subsys/tomcat_all
SCRIPT_LOC="/app/Tomcat_Maintain_Script"

case "$1" in
'start')
    if [ -f $LOCKFILE ]; then
        echo $0 already running.
        exit 1
    fi
        echo -n $"Starting tomcat_all....."	
	    nohup sh $SCRIPT_LOC/ROOR_sync.sh > /dev/null 2>&1 &
		nohup sh $SCRIPT_LOC/swap.sh > /dev/null 2>&1 &
		nohup sh $SCRIPT_LOC/upload_sync.sh > /dev/null 2>&1 &
		nohup sh $SCRIPT_LOC/Tomcat_Auto_Maintain.sh > /dev/null 2>&1 &
        touch $LOCKFILE
    ;;
'stop')
    if [ ! -f $LOCKFILE ]; then
        echo $0 already stopping.
		rm -rf $LOCKFILE
        exit 1
    fi
        echo -n $"Stopping tomcat_all......"
	    ps -ef | grep "ROOT_sync.sh" | awk '{print $2}' | xargs kill > /dev/null 2>&1 &
		ps -ef | grep "swap.sh" | awk '{print $2}' | xargs kill > /dev/null 2>&1 &
		ps -ef | grep "upload_sync.sh" | awk '{print $2}' | xargs kill > /dev/null 2>&1 &
		ps -ef | grep "Tomcat_Auto_Maintain.sh" | awk '{print $2}' | xargs kill > /dev/null 2>&1 &		
        rm -rf $LOCKFILE
    ;;
'restart')
    $0 stop
    $0 start
	echo -n $"Restarting tomcat_all......"
    ;;
'status')
    if [ -f $LOCKFILE ]; then
        echo $0 started.
    else
        echo $0 stopped.
    fi
    ;;
*)
    echo "Usage: $0 [start|stop|status]"
    exit 1
esac
exit 0
User avatar
nwclasantha
Posts: 75
Joined: Wed Apr 24, 2013 12:57 am
Location: Malabe

Re: Creating a Linux Service

Post by nwclasantha » Tue May 05, 2015 1:52 pm

Code: Select all

chmod +x /etc/rc.d/init.d/tomcat_all
chkconfig --add tomcat_all
chkconfig tomcat_all on
service tomcat_all start
Phonefix
Corporal
Corporal
Posts: 4
Joined: Mon Sep 25, 2017 4:14 pm
Location: United Kingdom

Re: Creating a Linux Service

Post by Phonefix » Mon Sep 25, 2017 4:33 pm

We need to create a Linux service which has to be run in the background. The service should run a java code.
Post Reply

Return to “Linux”