所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序。更通俗的讲,就是进程的开机启动。
本文主要介绍如何添加一个service。
1. 开发一个程序
首先开发一个软件,使其成为service。代码如下:
//capsule.c
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
int init_daemon();
int main()
{
init_daemon();
while(1)
{
sleep(2);
}
return 0;
}
int init_daemon()
{
int i;
pid_t pid;
if ( (pid = fork()) < 0)
{
return (-1);
}
else if (pid)
{
_exit(0); /* parent terminates */
}
/* child 1 continues... */
if (setsid() < 0) /* become session leader */
{
return (-1);
}
signal(SIGHUP, SIG_IGN);
if ( (pid = fork()) < 0)
{
return (-1);
}
else if (pid)
{
_exit(0); /* child 1 terminates */
}
chdir("/"); /* change working directory */
/* close off file descriptors */
for (i = 0; i < 255; i++)
{
close(i);
}
/* redirect stdin, stdout, and stderr to /dev/null */
open("/dev/null", O_RDONLY);
open("/dev/null", O_RDWR);
open("/dev/null", O_RDWR);
return (0);
}
init_daemon()完成进程的精灵化过程,包括脱离终端转入后台等。
编译
# gcc capsule.c –o capsule
将可执行文件拷贝到/usr/local/sbin目录下
# cp capsule /usr/local/sbin/
2.编写service脚本
service一般通过chkconfig工具进行管理。chkconfig管理的每个service需要在其init.d脚本中添加两行或更多注释行。
第一行告诉chkconfig 该service运行的默认级别,以及启动和停止的优先权。如果该service不在任何运行级启动,可以设置为”-“。
第二行包含描述信息。
针对刚才的可执行文件,编写service脚本capsuled如下:
#!/bin/bash
#
# capsuled A test service program
#
# chkconfig: - 92 12
# description: A test service prog
#
# @name: capsuled
# @author: lanyang
# @created: 2017.01.23
#
# Source function library.
. /etc/init.d/functions
PROG=capsuled
RETVAL=0
FULL_PATH=/usr/local/sbin/capsule
start()
{
echo -n $"Starting $PROG ..."
daemon $FULL_PATH
RETVAL=$?
echo
}
stop()
{
echo -n $"Stopping $PROG ..."
killproc $FULL_PATH
RETVAL=$?
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
RETVAL=$?
;;
status)
status $FULL_PATH
RETVAL=$?
;;
*)
echo $“Usage: $0 {start|stop|restart|status}”
exit 1
esac
exit $RETVAL
3.在/etc/init.d/目录下添加service脚本
# cp capsuled /etc/init.d
# chmod a+x capsuled
4.添加service,使chkconfig命令可以管理该service
# chkconfig --add capsuled
5.设置service启动运行级别
# chkconfig --level 2345 capsuled on
查看启动信息
# chkconfig --list capsuled
capsuled 0:off 1:off 2:on 3:on 4:on 5:on 6:off
6.启动service
# service capsuled start
Starting capsuled ... [ OK ]
# ps -ef | grep capsule
root 17729 1 0 18:31 ? 00:00:00 /usr/local/sbin/capsule
# service capsuled status
capsule (pid 17729) is running...
# service capsuled stop
Stopping capsuled ... [ OK ]
7.小结
# service capsuled start
与
# /etc/init.d/capsuled start
作用是一样的。实际上,前者是通过调用后者实现的。
管理service使用的chkconfig是一个用于维护/etc/rc[0-6].d目录的命令行工具。其中,[0-6]指的是系统的7个运行级别。
/etc/rc[0-6].d目录,内容全部是链接(symbolic links),一般链接到/etc/init.d/目录下的某个service脚本文件。
例如,
$ ll /etc/rc5.d/S85httpd
lrwxrwxrwx. 1 root root 15 Jul 30 2015 /etc/rc5.d/S85httpd -> ../init.d/httpd
其中,85是启动优先级;
$ ll /etc/rc6.d/K15httpd
lrwxrwxrwx. 1 root root 15 Jul 30 2015 /etc/rc6.d/K15httpd -> ../init.d/httpd
其中,15是停止优先级。
类似的,
# ll /etc/rc4.d/ | grep capsuled
lrwxrwxrwx. 1 root root 18 Jan 23 17:53 S92capsuled -> ../init.d/capsuled
# ll /etc/rc5.d/ | grep capsuled
lrwxrwxrwx. 1 root root 18 Jan 23 17:53 S92capsuled -> ../init.d/capsuled
92是启动优先级
# ll /etc/rc6.d/ | grep capsuled
lrwxrwxrwx. 1 root root 18 Jan 23 17:52 K12capsuled -> ../init.d/capsuled
12是停止优先级
关于chkconfig的使用,可以参考文章http://blog.csdn.net/lanyang123456/article/details/54695567