How to write a script in Linux start and stop with Xshell [A]

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u012431703/article/details/99285969

How to write a script start and stop start-stop script ------- [A] single project under Linux with Xshell


We, as farmers code in Linux environment deployment project is commonplace thing, so we often need to manually write their own some small start / stop / restart script; I simply followed for you on a shell script, you if you have any do not understand, welcome message, there is something wrong, correct me Comments are also welcome.

Shell Script profile:

Shell scripts like under Windows / Dos batch, which is the use of various types of commands previously placed into a file, a program file to facilitate the implementation of a one-time, mainly to facilitate the administrator to set up or manage used. But it is more powerful than the next batch of Windows, a program efficiency is higher than in other programming editing program, it uses a command in Linux / Unix.

shell and shell scripts What is the difference?

Shell itself is written in C language program that the user is using Linux bridges. The exact point that, Shell is a command-line interpreter, its role is to follow a certain command syntax will be explained and passed to the input of the system. It provides an interface for system-level program sends a request to run a program to Linux users, the user can use the Shell to start, suspend, stop or even write some programs. Shell is both a command language is a programming language (what you call a shell script). As a command language commands which interpret and execute interactive user input; as a programming language that defines variables and parameters, and provides a number of control structures that has the higher order language, including looping and branching. Although it is not part of the Linux kernel, but it calls most of the functionality of the system kernel to execute programs, create documents and in a parallel fashion coordinator to run each program.

shell script: Open a text editor (you can use vi / vim command to create the file), create a new file test.sh, extension sh (sh behalf shell), the extension does not affect the script execution, see names like EENOW If you write a shell script using php, php extension on the use of good.

#!/bin/bash                    "#!" 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种Shell。
echo "Hello World !"            echo命令用于向窗口输出文本。

Writing a script:

#! / bin / bash
# here can be replaced with your own implementation of the program, other code without changing
APP_NAME = / usr / Software / the LIN / Project / stopcar / Server-Eureka-1.0.0.jar
 
# Instructions for tips input parameters
Usage () {
    echo "the Usage: SH execution script .sh [Start | STOP | the restart | Status]"
    Exit. 1
}
 
# check program is running
is_exist () {
  PID = `PS -ef | grep $ APP_NAME | grep grep -v | awk 'Print $ {2}' `
  # returns 1 if there is, exists returns 0     
  IF [the -Z" PID $ {} "]; the then
   return 1
  the else
    return 0
  Fi
}
 
# startup method
start () {
  is_exist
  IF [? $ -eq "0"]; the then
    echo. ". $ {IS} APP_NAME already running PID PID = $ {}"
  the else
    nohup java -jar $APP_NAME >> nohup.out 2>&1 &
    echo "...start OK!"
  fi
}
 
#停止方法
stop(){
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
    echo "...stop OK!"
  else
    echo "${APP_NAME} is not running"
  fi  
}
 
#输出运行状态
status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is running. Pid is ${pid}"
  else
    echo "${APP_NAME} is NOT running."case "$ 1" in# The input parameter, performing the corresponding selected method is executed instructions do not enter}   Start   STOPthe restart () {# restart}
  Fi

 





 


  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  *)
    usage
    ;;
esac

Write a script startup success stories:

Script writing II:

#!/bin/bash
source /etc/profile
# chkconfig: - 57 75
# description: server-eureka

# Check that networking is up.
[[ ${NETWORKING} = "no" ]] && exit 1

start() {
echo "Starting server-eureka ..."
nohup java -jar -Xss768K -Xms1536m -Xmx1536m -Xmn1068m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 /home/park/server-eureka/server-eureka-1.0.0.jar &
#echo 3 >> /tmp/sunyu.txt
}

stop() {
echo "Stopping server-eureka ..."
kill -9 $(ps -ef | grep server-eureka-1.0.0 | grep -v grep | awk '{print $2}')
#echo 2 >> /tmp/sunyu.txt
}

# See how we were called.
case "$1" in
  start)
start
;;
  stop)
stop
;;
  restart|force-reload)
stop
start
;;
  *)
echo $"Usage: $0 {start|stop|restart|force-reload}"
exit 2
esac

脚本文件编写好了,首先得给我们的脚本文件通过命令【chmod 700 文件名】授予权限,权限授权成功则为绿色,否则白色,只有授权成功的文件,后续才可执行成功。

只需要执行命令【./脚本文件名.sh start】,则会自动生成【nohup.out】日志文件

输入命令【ps -ef|grep eureka】可以通过端口号查看是否 启动成功

输入命令【cat nohup.out】即可查看启动日志

以上的脚本写法一和脚本写法二是专门针对单个项目的启停。


下一篇 ----《如何用Xshell在Linux下编写启停脚本【二】》---- 多个项目的启停脚本的编写


 

Guess you like

Origin blog.csdn.net/u012431703/article/details/99285969