How to write a universal Java program startup script (Linux)?
脚本代码如下:
#!/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;
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

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.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)
