CentOS7 添加自定义系统服务案例
2016-09-14 21:46
393 查看
示例一:
执行脚本/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 fimyservice.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 添加自定义系统服务案例
- CentOS7利用systemctl添加自定义系统服务
- CentOS7利用systemctl添加自定义系统服务
- CentOS7利用systemctl添加自定义系统服务
- CentOS7添加自定义系统服务systemctl java jar
- CentOS7设置自定义开机启动,添加自定义系统服务
- CentOS7利用systemctl添加自定义系统服务
- CentOS7设置自定义开机启动脚本,添加自定义系统服务
- Ubuntu15.x /CentOS 7.x 以后 设置开机启动,添加自定义系统服务,自定义开机启动
- [实践] Android5.1.1源码 - 在Framework中添加自定义系统服务
- 在CentOS 7上利用systemctl添加自定义系统服务
- CentOS 7 systemd添加自定义系统服务
- 在CentOS 7上利用systemctl添加自定义系统服务
- [实践] Android5.1.1源码 - 在Framework中添加自定义系统服务
- 在CentOS 7上利用systemctl添加自定义系统服务
- 在CentOS 7上利用systemctl添加自定义系统服务
- CentOS 7.X设置自定义开机启动,添加自定义系统服务
- 在CentOS 7上利用systemctl添加自定义系统服务
- CentOS 7上利用systemctl添加自定义系统服务
- CentOS 7.x设置自定义开机启动,添加自定义系统服务