Home Java javaTutorial How to write a universal Java program startup script (Linux)?

How to write a universal Java program startup script (Linux)?

May 07, 2023 pm 05:22 PM
linux java

脚本代码如下:

#!/bin/sh
#该脚本为linux下启动java程序的通用脚本。即可以作为开机自启动service脚本被调用,
#也可以作为启动java程序的独立脚本来使用。
#
#author: tudaxia.com, date: 2011/6/7
#
#警告!!!:该脚本stop部分使用系统kill命令来强制终止指定的java程序进程。
#在杀死进程前,未作任何条件检查。在某些情况下,如程序正在进行文件或数据库写操作,
#可能会造成数据丢失或数据不完整。如果必须要考虑到这类情况,则需要改写此脚本,
#增加在执行kill命令前的一系列检查。
#
#
###################################
#环境变量及程序执行参数
#需要根据实际环境以及java程序名称来修改这些参数
###################################
#jdk所在路径
java_home="/usr/java/jdk"
 
#执行程序启动所使用的系统用户,考虑到安全,推荐不使用root帐号
running_user=root
 
#java程序所在的目录(classes的上一级目录)
app_home=/opt/tudaxia/test/web-inf
 
#需要启动的java主程序(main方法类)
app_mainclass=com.tudaxia.test.testmain
 
#拼凑完整的classpath参数,包括指定lib目录下所有的jar
classpath=$app_home/classes
for i in "$app_home"/lib/*.jar; do
  classpath="$classpath":"$i"
done
 
#java虚拟机启动参数
java_opts="-ms512m -mx512m -xmn256m -djava.awt.headless=true -xx:maxpermsize=128m"
 
###################################
#(函数)判断程序是否已启动
#
#说明:
#使用jdk自带的jps命令及grep命令组合,准确查找pid
#jps 加 l 参数,表示显示java的完整包路径
#使用awk,分割出pid ($1部分),及java程序名称($2部分)
###################################
#初始化psid变量(全局)
psid=0
 
checkpid() {
  javaps=`$java_home/bin/jps -l | grep $app_mainclass`
 
  if [ -n "$javaps" ]; then
   psid=`echo $javaps | awk '{print $1}'`
  else
   psid=0
  fi
}
 
###################################
#(函数)启动程序
#
#说明:
#1. 首先调用checkpid函数,刷新$psid全局变量
#2. 如果程序已经启动($psid不等于0),则提示程序已启动
#3. 如果程序没有被启动,则执行启动命令行
#4. 启动命令执行后,再次调用checkpid函数
#5. 如果步骤4的结果能够确认程序的pid,则打印[ok],否则打印[failed]
#注意:echo -n 表示打印字符后,不换行
#注意: "nohup 某命令 >/dev/null 2>&1 &" 的用法
###################################
start() {
  checkpid
 
  if [ $psid -ne 0 ]; then
   echo "================================"
   echo "warn: $app_mainclass already started! (pid=$psid)"
   echo "================================"
  else
   echo -n "starting $app_mainclass ..."
   java_cmd="nohup $java_home/bin/java $java_opts -classpath $classpath $app_mainclass >/dev/null 2>&1 &"
   su - $running_user -c "$java_cmd"
   checkpid
   if [ $psid -ne 0 ]; then
     echo "(pid=$psid) [ok]"
   else
     echo "[failed]"
   fi
  fi
}
 
###################################
#(函数)停止程序
#
#说明:
#1. 首先调用checkpid函数,刷新$psid全局变量
#2. 如果程序已经启动($psid不等于0),则开始执行停止,否则,提示程序未运行
#3. 使用kill -9 pid命令进行强制杀死进程
#4. 执行kill命令行紧接其后,马上查看上一句命令的返回值: $?
#5. 如果步骤4的结果$?等于0,则打印[ok],否则打印[failed]
#6. 为了防止java程序被启动多次,这里增加反复检查进程,反复杀死的处理(递归调用stop)。
#注意:echo -n 表示打印字符后,不换行
#注意: 在shell编程中,"$?" 表示上一句命令或者一个函数的返回值
###################################
stop() {
  checkpid
 
  if [ $psid -ne 0 ]; then
   echo -n "stopping $app_mainclass ...(pid=$psid) "
   su - $running_user -c "kill -9 $psid"
   if [ $? -eq 0 ]; then
     echo "[ok]"
   else
     echo "[failed]"
   fi
 
   checkpid
   if [ $psid -ne 0 ]; then
     stop
   fi
  else
   echo "================================"
   echo "warn: $app_mainclass is not running"
   echo "================================"
  fi
}
 
###################################
#(函数)检查程序运行状态
#
#说明:
#1. 首先调用checkpid函数,刷新$psid全局变量
#2. 如果程序已经启动($psid不等于0),则提示正在运行并表示出pid
#3. 否则,提示程序未运行
###################################
status() {
  checkpid
 
  if [ $psid -ne 0 ]; then
   echo "$app_mainclass is running! (pid=$psid)"
  else
   echo "$app_mainclass is not running"
  fi
}
 
###################################
#(函数)打印系统环境参数
###################################
info() {
  echo "system information:"
  echo "****************************"
  echo `head -n 1 /etc/issue`
  echo `uname -a`
  echo
  echo "java_home=$java_home"
  echo `$java_home/bin/java -version`
  echo
  echo "app_home=$app_home"
  echo "app_mainclass=$app_mainclass"
  echo "****************************"
}
 
###################################
#读取脚本的第一个参数($1),进行判断
#参数取值范围:{start|stop|restart|status|info}
#如参数不在指定范围之内,则打印帮助信息
###################################
case "$1" in
  'start')
   start
   ;;
  'stop')
   stop
   ;;
  'restart')
   stop
   start
   ;;
  'status')
   status
   ;;
  'info')
   info
   ;;
 *)
   echo "usage: $0 {start|stop|restart|status|info}"
   exit 1
esac;
Copy after login

The above is the detailed content of How to write a universal Java program startup script (Linux)?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

How to back up vscode settings and extensions How to back up vscode settings and extensions Apr 15, 2025 pm 05:18 PM

How to back up VS Code configurations and extensions? Manually backup the settings file: Copy the key JSON files (settings.json, keybindings.json, extensions.json) to a safe location. Take advantage of VS Code synchronization: enable synchronization with your GitHub account to automatically back up all relevant settings and extensions. Use third-party tools: Back up configurations with reliable tools and provide richer features such as version control and incremental backups.

See all articles