Taking Redis service as an example, this paper introduces two methods of service self-starting SERVICE,SYSTEMCTL use example
1. Modify redis.conf to allow background operation
Daemonize no change to daemonize Yes
2. Using the service command
1) Write the startup shell script named Redis
The Redis file code is as follows:
#!/bin/sh#configurations injected by Install_server below .... Exec=/home/shijingjing/redis-4.0.2/src/redis-server # Redis-server's Path cliexec=/home/shijingjing/redis-4.0.2/src/ REDIS-CLI # Redis-cli's Path Pidfile=/var/run/redis_6379.pid # redis.conf has this parameter, copy it over conf= "/home/shijingjing/ redis-4.0.2/redis.conf "# redis.conf path redisport=" 6379 "# port ################ SysV Init information# chkconfig:-58 74# Description:redis is the Redis daemon.### BEGIN INIT info# provides:redis# required-start: $network $local _fs $remote _fs # required-stop: $network $local _fs $remote _fs# default-start:2 3 4 * default-stop:0 1 6# should-start: $syslog $named # Should-stop: $syslog $named # Short-description:start and Stop redis# description:redis daemon### END INIT infocase "$" in start) if [-f $PIDFILE] then echo ' $PIDFILE exists, process is already running or Crashe D "Else echo" starting Redis server ... "$EXEC $CONF fi;; Stopif [!-F $PIDFILE] then echo ' $PIDFILE does not exist, process was not running "else pid=$ (cat $PIDFILE) echo "Stopping ..." $CLIEXEC-p $REDISPORT shutdown while [ -X/PROC/${PID}] do echo "waiting-Redis to shutdown ..." Sleep 1 Done echo "Redis stopped" FI;; Status) pid=$ (cat $PIDFILE) if [!-x/proc/${pid}] then echo ' Redis was not running ' else echo "Redis is running ($PID)" FI;; Restart) $ stop $ start; *) echo "Please use start, stop, restart or status as first argument";; Esac
Place the Redis file in the/etc/init.d folder or copy the Utils/redis_init_script file from the installation directory directly to the directory
Change file to Executable
chmod +x redis
2) Put the execution file in, remove boot from boot
update-rc.d redis defaultsupdate-rc.d redis remove
3) manual start off Redis
/etc/init.d/redis start/etc/init.d/redis stop/etc/init.d/redis restart
3. Using the SYSTEMCTL command
1) write the. service file, Redis.service
[Unit]Description=redis[Service]PIDFile=/var/run/redis.pidExecStart=/home/shijingjing/redis-4.0.2/src/redis-serverRestart=on-failureRestartSec=42s[Install]WantedBy=multi-user.target
Put Redis.service into the/etc/systemd/system folder
2) Add service, remove boot from boot
systemctl enable redissystemctl disable redis
3) manual start off Redis
systemctl start redissystemctl stop redissystemctl restart redis
4.service and Systemctl Comparison
SYSTEMD-compatible service, more powerful
Writing service startup scripts is also more concise, service needs to write a complete shell script, systemd only need to configure the execution file, PID file path, the rest to Systemd to do it
Linux service boot-up use example