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