用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(三)
来源:互联网 发布:数据可视化工程师培训 编辑:程序博客网 时间:2024/06/03 22:43
用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(三)
本文提供了Daemon程序的SHELL管理脚本的一个简单例子,其他相关文章请见:
用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(一)
用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(二)
1、首先(一)中所述的代码中添加如下内容:
追加头文件引用:
#include <fstream>
在umask(0);之前添加如下代码:
std::ofstream _out("D_test.pid", std::ios_base::out);
_out << getpid() << std::endl;
_out.close();
2、编写如下脚本,并保存为D_test.sh:
#!/bin/bash
#service_exe_file= 'D_test'
#service_pid_file = 'D_test.pid'
service_pid =
service_wait_for_pid()
{
verb="$1"
pid_file="$2"
time_out="$3"
i=0
sleep 1
while ["$i" -ne "$time_out"]
do
case "$verb" in
'created')
test -s "$pid_file" && i = " && break
;;
'removed')
test ! -s "$pid_file" && i = "" && break
;;
*)
echo "Usage: wait_for_pid created/removed pidfile timeout"
exit 1
;;
esac
i=$((i+1))
sleep 1
done
if [-z "$i"]
then
return 0
fi
return 1
}
service_usage()
{
echo "Usage: D_test.sh start|stop|status"
}
service_status()
{
ps aux | grep "$service_exe_file" | grep -c 'grep'
if [-n "$service_pid"]
then
kill -SIGUSR1 $service_pid
sleep 1
fi
}
service_start()
{
if [-n "$service_pid"]
then
echo "this service is already is running."
return 1
fi
"$service_exe_file"
service_wait_for_pid created "$service_pid_file" 60
service_status
}
service_stop()
{
if [-n "$service_pid"]
then
kill -SIGTERM $service_pid
service_wait_for_pid removed "$service_pid_file" 60
fi
ps aux | grep "$service_exe_file" | grep -V 'grep'
}
if [$# -ne 1]
then
service_usage
exit 1
fi
if [-s "$service_pid_file"]
then
service_pid=$(case "$service_pid_file")
fi
case "$1" in
'start')
service_start
;;
'stop')
service_stop
;;
'status')
service_status
;;
*)
service_usage
;;
esac
至此全文完
- 用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(三)
- 用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(一)
- 用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(二)
- linux Daemon程序设计(含步骤、源码、管理)
- C程序调用shell脚本的三种方法
- 一个Linux shell脚本,关于CI上打包的
- Tomcat创建实例的一个脚本(shell 脚本)
- linux下c程序 daemon、fork与创建pthread的顺序问题
- linux的Shell脚本程序
- Linux shell 脚本(三)
- 用php写的一个daemon程序
- 一个创建工程目录的shell脚本
- 一个用shell写的执行java程序的脚本
- 一个简单的shell脚本程序(运用expr命令)
- 一个启动Java程序的shell脚本
- linux shell脚本中读取文件内容的方法(含效率测试)
- linux的C程序 调用 shell脚本,获取shell的执行结果
- 管理Tomcat服务的Daemon脚本
- window基本功能介绍
- 如何在Google成为一名优秀的产品经理?
- EnumChildWindows的使用
- Qt网络编程
- 打包安装
- 用c++创建一个Linux上的Daemon程序,含SHELL管理脚本(三)
- 深度优先搜索 广度优先搜索类训练题
- Mapper与Reducer的链接
- fatal error CVT1100
- winCE6.0 BSP 标准目标结构
- IT人士必去的10个网站
- 6410NandFlash 挂了
- 不知道要怎么进入工作
- make下的有效文件