CentOS7 添加自定义系统服务案例

示例一:

执行脚本/root/project/systemctl/test.sh()

#########################################################################
# File Name   : test.sh
# Author      : Youner Liu
# Mail        : [email protected]
# Created Time: Wed 14 Sep 2016 07:13:37 PM CST
# Description : 
#########################################################################
#!/bin/bash

function test()
{
	count=0
	num=3000
	while [ ${count} -lt ${num} ]
	do
		if [ -f /root/project/systemctl/stop ]; then
			break
		fi
		count=$((count+1))
		date >> /root/project/systemctl/test.log
		echo "hello world ${count}" >> /root/project/systemctl/test.log
		sleep 1
	done
}

if [ ${1} == "start" ]; then
	test;
elif [ ${1} == "stop" ];then
	echo "touch  /root/project/systemctl/stop"
	touch  /root/project/systemctl/stop
fi
myservice.service源码(/usr/lib/systemd/system/myservice.service)
[Unit]
Description=my service
 
[Service]
Type=forking
PIDFile=/root/project/systemctl/myservice.pid
ExecStart=/usr/bin/sh /root/project/systemctl/test.sh start &
ExecStop=/usr/bin/sh /root/project/systemctl/test.sh stop
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

测试:

[root@node0 systemctl]# systemctl start myservice.service
Job for myservice.service failed because a timeout was exceeded. See "systemctl status myservice.service" and "journalctl -xe" for details.
[root@node0 systemctl]# systemctl status myservice.service
● myservice.service - nginx - my service
   Loaded: loaded (/usr/lib/systemd/system/myservice.service; disabled; vendor preset: disabled)
   Active: failed (Result: timeout) since Wed 2016-09-14 21:40:46 CST; 44s ago
  Process: 7459 ExecStart=/usr/bin/sh /root/project/systemctl/test.sh start & (code=killed, signal=TERM)

Sep 14 21:39:16 node0 systemd[1]: Starting nginx - my service...
Sep 14 21:40:46 node0 systemd[1]: myservice.service start operation timed out. Terminating.
Sep 14 21:40:46 node0 systemd[1]: Failed to start nginx - my service.
Sep 14 21:40:46 node0 systemd[1]: Unit myservice.service entered failed state.
Sep 14 21:40:46 node0 systemd[1]: myservice.service failed.
[root@node0 systemctl]# cat /root/project/systemctl/test.log 

Wed Sep 14 21:39:16 CST 2016
hello world 1
....
Wed Sep 14 21:40:45 CST 2016
hello world 89
Wed Sep 14 21:40:46 CST 2016
hello world 90
[root@node0 systemctl]# 

systemctl start myservice.service卡住90秒;然后报出timeout

这个问题应该如何解决那????

整改

脚本/root/project/systemctl/test_start.sh

#########################################################################
# File Name   : test_start.sh
# Author      : Youner Liu
# Mail        : [email protected]
# Created Time: Wed 14 Sep 2016 07:13:37 PM CST
# Description : 
#########################################################################
#!/bin/bash

function test()
{
	count=0
	num=3000
	echo > /root/project/systemctl/test.log
	while [ ${count} -lt ${num} ]
	do
		if [ -f /root/project/systemctl/stop ]; then
			rm -rf /root/project/systemctl/stop;
			break
		fi
		count=$((count+1))
		date >> /root/project/systemctl/test.log
		echo "hello world ${count}" >> /root/project/systemctl/test.log
		sleep 1
	done
}

test


脚本/root/project/systemctl/test_stop.sh
#########################################################################
# File Name   : test_stop.sh
# Author      : Youner Liu
# Mail        : [email protected]
# Created Time: Wed 14 Sep 2016 07:13:37 PM CST
# Description : 
#########################################################################
#!/bin/bash

echo "touch  /root/project/systemctl/stop"
touch  /root/project/systemctl/stop

脚本/root/project/systemctl/test.sh

#########################################################################
# File Name   : test.sh
# Author      : Youner Liu
# Mail        : [email protected]
# Created Time: Wed 14 Sep 2016 07:13:37 PM CST
# Description : 
#########################################################################
#!/bin/bash

if [ ${1} == "start" ]; then
	sh /root/project/systemctl/test_start.sh &
elif [ ${1} == "stop" ];then
	sh /root/project/systemctl/test_start.sh &
fi

myservice.service源码(/usr/lib/systemd/system/myservice.service)
[Unit]
Description=my service
 
[Service]
Type=forking
#PIDFile=/root/project/systemctl/myservice.pid
ExecStart=/usr/bin/sh /root/project/systemctl/test.sh start
ExecStop=/usr/bin/sh /root/project/systemctl/test.sh stop
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

OK





你可能感兴趣的:(维护与工具)