CENTOS7 Create daemon with shell and set as system service, boot automatically start

Source: Internet
Author: User

The first is to create the shell daemon: (shouhu.sh)

#!/bin/bashtest () {        while true         do                 ls ./  >> 123.txt                 sleep 5                              done}case $1 instart)         path= "/root/shouhu.pid"         if [ -f  $path  ]; then                 pid= ' cat /root/ Shouhu.pid '                  echo  "Process $pid running ..."         else                 test &                  echo $! > /root/shouhu.pid                 echo  "Run process $!..."           fi        ;; Stop)         pid= ' Cat /root/shouhu.pid '          kill -9  $pid         echo  End Process $ PID ... "        rm -rf /root/shouhu.pid         ;; Restart)         path= "/root/shouhu.pid"          if [ -f  "$path"  ];then                 pid= ' Cat /root/shouhu.pid '                  kill -9  $pid                  echo  "End Process $pid ..."                  rm -rf /root/ Shouhu.pid                test  &                 echo $! > /root/shouhu.pid                 echo  "Run process $!..."         else                 echo  "Process not running ..."                  test &                 echo $! > /root/ Shouhu.pid                echo   "Run process $!..."         fi          ;; Status)         path= "/root/shouhu.pid"          if [ -f  $path  ];then                 pid= ' Cat /root/shouhu.pid '                  echo  "Process $pid is running ..."          else                 echo  "Process not running ..."         fi         ;; *)         echo  "Usage:{start|stop|restart|status}"          ;; Esac

Then set the shouhu.sh to system service:

#~]vi/usr/lib/systemd/system/shouhu.service
[Unit]  Description=shouhu After=network.target [Service] type=forking pidfile=/root/shouhu.pid execstart=/root/shouhu.sh startexecreload=/root/shouhu.sh restartexecstop=/root/shouhu.sh stopprivatetmp=true [Install] WantedBy=multi-user. Target

CentOS 7 Service Systemctl script is stored in:/usr/lib/systemd/, the system and users (user), need to boot the program can run without logging on, there are system services, namely:/usr/lib/systemd/ The system directory. Each service ends with a. Service and is typically divided into 3 parts: [Unit], [service], and [Install]

The [Unit] section is primarily a description of the service, which includes description and after,description used to describe the service, after which is used to set the boot order;

The [Service] section is the service of the key, is the service of some specific operating parameters of the settings, here type=forking is the form of the background run, Pidfile for the file path of the PID, Execstart for the operation of the service command, execreload Restart command, Execstop for the Stop command, Privatetmp=true represents a separate temporary space for the service, note that the start, restart, and stop commands in the [Service] section all require absolute paths, and the relative paths will cause errors;

The [Install] section is a set of settings for a service installation that can be set to multi-user.


Save the written file, and then set the boot up:

~]# Systemctl Enable Shouhu

This article is from the "11324936" blog, please be sure to keep this source http://11334936.blog.51cto.com/11324936/1925127

CENTOS7 Create daemon with shell and set as system service, boot automatically start

Related Article

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.