CentOS Linux program self-start script

  

program self-start script is essentially a shell script. Take a simple Tomcat self-starting script as an example. Tomcat uses startup.sh in the installation directory to start and shutdown.sh to stop. We can write them into a startup script.

Create a self-starting script:

vim /etc/init.d/tomcat

Enter the following:

#!/bin/bash## Tomcat startup script for the Tomcat server## chkconfig: 345 80 20# description: start the tomcat deamon## Source function library. /etc/rc.d/init.d/functions

prog=tomcatJAVA_HOME=/Usr/java/jdk1.6.0_27export JAVA_HOMECATALANA_HOME=/usr/local/tomcatexport CATALINA_HOME

case "$1" instart)echo "Starting Tomcat..."$CATALANA_HOME/bin/startup.sh; ;

stop)echo "Stopping Tomcat..."$CATALANA_HOME/bin/shutdown.sh;;

restart)echo "Stopping Tomcat..."$CATALANA_HOME/Bin/shutdown.shsleep 2echoecho "Starting Tomcat..."$CATALANA_HOME/bin/startup.sh;;

*)echo "Usage: $prog {start| Stop| Restart}";;esacexit 0 Description: The startup script here can be divided into three parts, the first part is to declare the startup script and comments, the second part is to define the path variable, the third part is a case... In condition selection structure.

The first part 1) Because it is a shell script, you must have a #!/bin/bash line at the beginning, which means that the shell used is bash. 2) #chkconfig: 345 80 20 is to let the chkconfig command recognize this startup script, it must be there, and the rest with # are comments. 3). /etc/rc.d/init.d/functions executes the functions file in the current shell, not in the subshell. It is similar to library functions, and later startup scripts may call the underlying functions in functions.

Second part 1) Starting from prog=tomcat, set 3 variables, use prog to define the script name, JAVA_HOME to define the JDK installation directory, and CATALANA_HOME to define the tomcat installation directory. 2) The export command is to make the defined variables available in the subshell.

The third part 1) The third part is a case condition selection structure, the syntax structure is as follows:

case string in value 1) operation;; value 2) operation;; value 3) Operation;;...*) Operation;;esac2)$1 is a positional parameter. The positional parameter is a variable that is determined according to its position on the command line of the calling shell program, and is a parameter that is entered after the program name. The positional parameters are separated by spaces. The shell takes the first position parameter to replace $1 in the program file, the second replaces $2, and so on. 3) $CATALANA_HOME/bin/startup.sh means to start tomcat. 4) $CATALANA_HOME/bin/shutdown.sh means to stop tomcat. 5) Sleep 2 means sleep for 2 seconds. 6) exit 0 means exit.

Copyright © Windows knowledge All Rights Reserved