יום חמישי, 30 במאי 2013

Starting stopping and restarting a java service app in bash

I found the following bash script used to allow stopping restarting and starting of an java application service:

  1: #!/bin/sh
  2: case "$1" in
  3: 
  4: 	start)
  5: 		echo "Start myService"
  6: 		cd /home/ubuntu/myService
  7: 		nohup java -cp .:Service-bin:lib/* com.myCompany.Services.myService > OutputData.log &
  8: 		sleep 3
  9: 		tail -n 10 OutputData.log
 10: 	;;
 11: 
 12: 	stop)
 13: 		echo "Stop service"
 14: 		kill $(ps aux | grep myService|awk '{print $2}') > /dev/null
 15: 	;;
 16: 
 17: 	restart)
 18: 		echo "Restart myService"
 19: 		$0 stop
 20: 		sleep 1
 21: 		$0 start
 22: 	;;
 23: esac
 24: 
 25: exit 0

Things I want to emphasize:
1.The restart case is reenter into the script using the $0
2.Running the service using the nohup bash command .The nohup bash command allows to run command./process or shell script that can continue running in the background after you log out from a shell.
3.The java –cp option allows to provide the classpath i.e. path(s) to additional classes or libraries that your program may require when being compiled or run.
4.Tail –n  10:prints only the final 10 lines of the log
5.The killing of the service line (line 14) list all the current process with the following columns:
USER = user owning the process
PID = process ID of the process
%CPU = It is the CPU time used divided by the time the process has been running.
%MEM = ratio of the process’s resident set size to the physical memory on the machine
VSZ = virtual memory usage of entire process
RSS = resident set size, the non-swapped physical memory that a task has used
TTY = controlling tty (terminal)
STAT = multi-character process state
START = starting time or date of the process
TIME = cumulative CPU time
COMMAND = command with all its arguments
 
we try to find the lines with the smyervice phrase and then use awk to get the 2nd word (the Pid that is going to be killed ).
We discard any output to directing the output to the null device .

אין תגובות:

הוסף רשומת תגובה