Linux centos7 production service self-starting systemd

Summary: - The first program of Centos7 boot is completely replaced from init to systemd, which is already a substantial difference from centos 5 and 6. systemd controls the boot service, boot level and other functions by managing the unit. The /usr/lib/systemd/system directory contains various unit files, service units with service suffixes, boot-level units with target suffixes, etc. Here, the files about service suffixes are temporarily introduced.

The first program of Centos7 boot is completely changed from init to systemd, which is already a substantial difference from centos 5 and 6. systemd controls the boot service, boot level and other functions by managing the unit.

The /usr/lib/systemd/system directory contains various unit files, service units with service suffixes, boot-level units with target suffixes, etc. Here, the files about service suffixes are temporarily introduced. Because systemd wants to perform self-starting at boot, the

specific

. Create a new service-name.service file in the /usr/lib/systemd/system directory
with apache's httpd.service Unit is explained as an example
[Unit] #Definition
Description
Description=The Apache HTTP Server #Specifies
that the service will be started after systemd executes those targets
After=network.target remote-fs.target nss-lookup.target

[Service]
#Define the running type of the Service, usually forking, which means running in the background
Type=notify
Environment=LANG=C
#The following defines each execution method of systemctl start |stop |reload *.service, the specific command # needs to write the absolute path
ExecStart=/ usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
# Send SIGWINCH for graceful stop
KillSignal=SIGWINCH
KillMode=mixed #Create
private memory temporary space
PrivateTmp=true

[Install]
WantedBy=multi -user.target
The following is a java project I wrote myself. I need a service that runs automatically after booting. Systemd will help me execute the script I wrote. The shell is as follows, for reference only.

#!/bin/bash
CMD=$1

case $CMD in
-start)

        nohup java -jar /home/mobileoa/apps/shMediaManage-0.0.1.jar &> /home/mobileoa/apps/shm.log &
        ;;
-stop)

        port=$(sudo netstat -tnlp | grep ':9099' |awk '{print $nf}' |awk -f'/' '{print $1}')
        sudo kill $port
        ;;

-restart)

        port=$(sudo netstat -tnlp | grep ':9099' |awk '{print $nf}' |awk -f'/' '{print $1}')
        sudo kill $port
        nohup java -jar /home/mobileoa/apps/shMediaManage-0.0.1.jar &> /home/mobileoa/apps/shm.log &
        ;;
*)
        echo "Usage: shMediaManager.sh -start | -stop | -restart .Or use systemctl start | stop | restart  MediaManager.service "
        ;;
esac
编辑.service file, execute the script under Exec* to start the shutdown and restart the service Description=Media wanager Service

[UNIT]

After=network.target

[Service]
Type=forking
**ExecStart=/home/mobileoa/apps/shMediaManager.sh -start
ExecReload=/home/mobileoa/apps/shMediaManager.sh -restart
ExecStop=/home/mobileoa/apps/ shMediaManager.sh -stop**
PrivateTmp=True

[Install]
WantedBy=multi-user.target set systemctl enable MediaManager.service will create a connection
from boot

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326437502&siteId=291194637
Recommended