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

本文介绍了一个关于Systemd服务启动时遇到超时错误的问题,并提供了详细的解决方案。通过调整脚本逻辑和配置文件设置,成功解决了服务启动过程中因长时间运行而引发的超时错误。

示例一:

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

#########################################################################
# File Name   : test.sh
# Author      : Youner Liu
# Mail        : younger_liucn@126.com
# 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        : younger_liucn@126.com
# 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        : younger_liucn@126.com
# 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        : younger_liucn@126.com
# 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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YoungerChina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值