示例一:

执行脚本/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

CentOS7 添加自定义系统服务案例的更多相关文章

  1. CentOS7利用systemctl添加自定义系统服务【转】

    systemctl enable name.service 设置开机启 systemctl disable name.service 删除开机启动指令 systemctl list-units --t ...

  2. [转]CentOS7利用systemctl添加自定义系统服务

    原文:https://www.cnblogs.com/saneri/p/7778756.html CentOS7自定义系统服务 CentOS7的服务systemctl脚本存放在:/usr/lib/sy ...

  3. CentOS7添加自定义脚本服务

    一.CentOS7添加自定义脚本服务说明 在CentOS7下,已经不再使用chkconfig命令管理系统开机自启动服务和条件自定义脚本服务了,而是使用管理unit的方式来控制开机自启动服务和添加自定义 ...

  4. [实践] Android5.1.1源码 - 在Framework中添加自定义系统服务

    前言 本文的方法要修改Android源码.但只要将系统服务写出来,也可以注入system_server进程将系统服务加载到system_server中. 步骤 1. 添加一个aidl文件,我将aidl ...

  5. Centos7 安装系统服务、开机自启动

    Centos7 安装系统服务 1 编写可执行程序 * 这里可以是任意编程语言(C.C++.Java.PHP.Python.Perl ...)编写的程序: 当前假设此程序的执行目录为: /myservi ...

  6. 在CentOS 7上利用systemctl添加自定义系统服务 /usr/lib/systemd/

    在CentOS 7上利用systemctl添加自定义系统服务[日期:2014-07-21] 来源:blog.csdn.net/yuanguozhengjust 作者:yuanguozhengjust ...

  7. systemctl添加自定义系统服务

    [Service] Type=forking ExecStart=绝对路径 ExecStop=绝对路径 ExecReload=绝对路径 以上最精简版,文件/usr/lib/systemd/system ...

  8. CentOS7 利用systemctl添加自定义系统服务

    一.命令systemctl介绍 CentOS 7.0中已经没有service命令,而是启用了systemctl服务器命令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 命 ...

  9. CentOS7利用systemctl添加自定义系统服务

    CentOS7的每一个服务以.service结尾,一般会分为3部分:[Unit].[Service]和[Install] 转载于互联网 [Unit] 部分主要是对这个服务的说明,内容包括Descrip ...

随机推荐

  1. (LaTex)CTex的初次使用心得及入门教程

    摘要 最近要发论文了,被知乎里人推荐使用论文编译软件(CTex.LaTex和Overleaf之类),瞬间感觉自己用Word简直Out了(书读少). 学校里也听说过LaTex,不过因为当时没怎么写过论文 ...

  2. 读REDIS数据结构

    一.DICT 主要有两个问题: 1.散列冲突,解决办法是拉链法 typedef struct dictEntry { void *key; union { void *val; uint64_t u6 ...

  3. 《编程导论(Java)·9.3.1回调·3》回调的实现

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/yqj2065/article/details/31441221 接<9.3.1Java回调 · ...

  4. Vue.js系列之vue-resource(6)

    网址:http://blog.csdn.net/u013778905/article/details/54235906

  5. Linux 安装ngnix

    第一步:把nginx的源码包上传到linux系统/home 第二步:解压缩 [root@localhost ~]# tar zxf nginx-1.8.0.tar.gz 第三步:使用configure ...

  6. 【洛谷P1538】迎春舞会之数字舞蹈

    迎春舞会之数字舞蹈 题目链接 一道奇怪的模拟题, 先将样例输出复制下来,观察观察 -- -- -- -- -- -- -- -- ① | | | | | | | | | | | | | | ② | | ...

  7. Entity Framework 六

    实体框架中的存储过程: 我们在创建edmx的时候把存储过程勾选了,所以在我们的上下文上中生成了方法. 存储过程为:就是需要显示多个表的字段.以往需要显示多个表的字段都是新建一个类把需要的字段当做属性写 ...

  8. vue使用v-for循环,动态修改element-ui的el-switch

    在使用element-ui的el-switch中,因为要用v-for循环,一直没有成功,后来仔细查看文档,发现可以这样写 <el-switch v-for="(item, key) i ...

  9. 为什么IP检验和发现错误直接丢弃而不是要求源站重发

    纠错控制由上层(传输层)执行IP首部中的源站地址也可能出错,请错误的源地址重传数据报是 没有意义的

  10. 動態SQL運用實例

    動態SQL運用實例 語法8.1.6之前: EXECUTE IMMEDIATE dynamic_sql_string [INTO {define_var1 [, define_var2] ... | p ...