Linux service self-starting configuration and adding scripts to the service

Linux service self-starting configuration and adding scripts to the service


  Previously, when the web system was deployed on a virtual machine for a customer, it was deployed on a desktop computer. The customer has a requirement that this computer may be turned off every day, and the virtual machine will also be turned on and off. , But the customer doesn't know how to start the virtual machine and the deployed service, so by the way, I summarized a simple configuration method for the service to start automatically.
  Take starting a textserver.jar service as an example. First of all, you need to configure the environment for starting the service, such as jdk. I won't talk about the installation and configuration of jdk here.
Next, you need to write a shell script:

[root@ecs-9fe2-0005 /]# vi /etc/rc.d/init.d/testserver

The content can refer to the script content of the startup .jar file below:

#!/bin/sh
#chkconfig: 2345 80 90
RESOURCE_NAME=testserver.jar
NAME=testserver.log
tpid=ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'
if [ ${tpid} ]; then
echo ‘Stop Process…’
kill -9 $tpid
fi
sleep 1
tpid=ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'
if [ ${tpid} ]; then
echo ‘Kill Process!’
kill -9 $tpid
else
echo ‘Stop Success!’
fi

tpid=ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'
if [ ${tpid} ]; then
echo ‘App is running.’
else
echo ‘App is NOT running.’
fi

rm -f tpid
nohup java -Xms512m -Xmx512m -Xmn128m -XX:PermSize=128m -XX:MaxPermSize=512m -jar /opt/test/server/jar/testserver.jar > /opt/test/server/log/$NAME&
echo $! > tpid
echo Start Success!
###end

Note that this sentence must have: #chkconfig: 2345 80 90 The
  script is written directly in the /etc/rc.d/init.d/ folder, and the /etc/rc.d/init.d/ directory is the linu system setting The startup directory of some services. You can also write it in other directories and cp to this directory. Here you need to ensure that the .jar file is executable and can be started successfully, you can try it in advance through the java -jar file name.jar. You can also try to see if the jar service can run successfully by executing the script file. If it can't run, then the configuration auto-start is useless. If it can run successfully, continue to the next step.
Continue to enter the following command to add permissions to the file so that the script file can be executed:
root@ecs-9fe2-0003 ~]# chmod 755 /etc/rc.d/init.d/testserver

Add files to the service
root@ecs-9fe2-0003 ~]# chkconfig --add /etc/rc.d/init.d/testserver

Note that the directories written in the shell script that need to be executed must be given directory permissions, otherwise an error such as file inaccessibility will be reported.

After the configuration is complete, you can first test whether it has been added to the service through the command, the command is as follows:
root@ecs-9fe2-0003 ~]# service testserver start

After execution, check the port or log to verify whether it takes effect and the service runs successfully.

The command to stop the service is:
root@ecs-9fe2-0003 ~]# service testserver stop

After that, you can restart the server for testing to verify whether the auto-start configuration is successful.

Guess you like

Origin blog.csdn.net/weixin_38829640/article/details/114405358