Java program in CENTOS7 boot from

Source: Internet
Author: User

1. Let's start with a simple start,status,stop program:

[Email protected] ~]# cat/home/tomcat/jarservice.sh
#!/bin/bash
Cu_pid=/home/tomcat/cupid.pid
Start ()
{
Echo-n "Start Java"
Nohup Java-jar/home/tomcat/exchange-1.4.jar >/dev/null 2>&1 &
new_agent_pid=$!
echo "$new _agent_pid" > $CU _pid
}
Stop ()
{
if [-f/home/tomcat/cupid.pid];then
Spid= ' Cat/home/tomcat/cupid.pid '
If ["$SPID"! = ""];then
Kill-9 $SPID
echo > $CU _pid
echo "Stop sucess"
Fi
Fi
}
Checkprocessstata ()
{
Cps_pid=$1
If ["$CPS _pid"! = ""]; then
Cps_pidlist= ' ps-ef|grep $CPS _pid|grep-v grep|awk-f "' {print $} '
Else
cps_pidlist= ' Ps-ef|grep ' $CPS _pname "|grep-v grep|awk-f" ' {print $} '
Fi

For cps_i in ' echo $CPS _pidlist '
Do
If ["$CPS _pid" = ""]; then
cps_i1= "$CPS _pid"
Else
cps_i1= "$CPS _i"
Fi

If ["$CPS _i1" = "$CPS _pid"]; then
#kill-S 0 $CPS _i
kill-0 $CPS _i >/dev/null 2>&1
If [$?! = 0]; then
echo "[' Date '] mc-10500:process $i have Dead"
Kill-9 $CPS _i >/dev/null 2>&1

Return 1
Else
#echo "[' Date '] mc-10501:process is Alive"
return 0
Fi
Fi
Done
echo "[' Date '] mc-10502:process $CPS _i is not exists"
Return 1
}

Status ()
{
Spid= ' Cat/home/tomcat/cupid.pid '
Checkprocessstata $SPID >/dev/null
If [$?! = 0];then
echo "unixdialup:{$SPID} Stopped ..."
Else
echo "unixdialup:{$SPID} Running Normal."
Fi

}

Restart ()
{
echo "Stoping ... "
Stop
echo "Staring ..."
Start
}

Case "$" in
Start
Start
;;
Stop
Stop
;;
Status
Status
;;
Restart
Restart
;;
*)
echo $ "Usage: $ {Start|stop|restart}"
Retval=1
Esac
Exit $RETVAL

2. Write a service file again:

[Email protected] ~]# Cat/lib/systemd/system/jar.service
[Unit]
Description=java
After=network.target

[Service]
Type=forking
execstart=/home/tomcat/jarservice.sh start
execreload=/home/tomcat/jarservice.sh restart
execstop=/home/tomcat/jarservice.sh stop
#PrivateTmp =true
User=tomcat
[Install]
Wantedby=multi-user.target
[Email protected] ~]#

3. Boot test: Success!!!

[Email protected] ~]# systemctl status Jar.service
Jar.service-java
Loaded:loaded (/usr/lib/systemd/system/jar.service; enabled; vendor preset:disabled)
Active:active (running) since Thu 2017-07-27 01:10:38 EDT; 36s ago
process:858 execstart=/home/tomcat/jarservice.sh Start (code=exited, status=0/success)
Main pid:870 (Java)
CGroup:/system.slice/jar.service
└─870 Java-jar/home/tomcat/exchange-1.4.jar

Java program in CENTOS7 boot from

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.