示例一:

执行脚本/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. libevent将信号封装为socket通知的核心代码

    #include"stdafx.h" #include"iostream" #include "algorithm" #include&qu ...

  2. HDU 4757 Tree(可持续化字典树,lca)

    题意:询问树上结点x到结点y路上上的权值异或z的最大值. 任意结点权值 ≤ 2^16,可以想到用字典树. 但是因为是询问某条路径上的字典树,将字典树可持续化,字典树上的结点保存在这条路径上的二进制数. ...

  3. pthread的各种同步机制

    https://casatwy.com/pthreadde-ge-chong-tong-bu-ji-zhi.html pthread是POSIX标准的多线程库,UNIX.Linux上广泛使用,wind ...

  4. csu 1947 三分

    题意: 长者对小明施加了膜法,使得小明每天起床就像马丁的早晨一样. 今天小明早上6点40醒来后发现自己变成了一名高中生,这时马上就要做早操了,小明连忙爬起来 他看到操场密密麻麻的人,突然灵光一闪想到了 ...

  5. c语言描述的静态查找表

    顺序表的查找: 直接循环依次和目标比较就行 有序表的查找(二分查找): int search(SS *T,Type key){ int mid; ; int high=T.length; while( ...

  6. select 文字右对齐

    select { direction: rtl; } select option { direction: ltr; }

  7. ACL常用命令及工作原理

    What ACL 是一系列 IOS 命令,根据数据包报头中找到的信息来控制路由器应该转发还是应该丢弃数据包.ACL 是思科 IOS 软件中最常用的功能之一. 在配置后,ACL 将执行以下任务: 限制网 ...

  8. oracle 监听服务配置

    最近在red hat 6.6虚拟机上安装了Oracle 11gR2数据库,安装完毕,使用没有问题,通过主机也可以访问到虚拟机上的数据库.然而,在重新启动虚拟机后,主机无法访问到数据库,提示错误: PS ...

  9. iOS 获取APP的CPU、内存等信息

    目标是开发一个SDK,嵌入到APP里面,用来统计当前APP的实时CPU.内存等信息 2015.11.17 http://stackoverflow.com/questions/12889422/ios ...

  10. iOS之UIKeyboardType 11种键盘图片展示

    UIKeyboardTypeDefault      UIKeyboardTypeASCIICapable  ==  UIKeyboardTypeAlphabet      UIKeyboardTyp ...