Linux守护进程启动

我在linux上写了一个服务(Redhat Server Edition 5.1)。 当我开始我的应用程序我手动启动我的服务,现在我想启动我的服务在启动时,通过手段我把我的服务在init.d文件夹我的守护进程不启动在启动时调用,任何有想法如何启动一个守护进程在Linux上启动时?

这是我的示例,但不工作

#!/bin/sh # # myservice This shell script takes care of starting and stopping # the <myservice> # # Source function library . /etc/rc.d/init.d/functions # Do preliminary checks here, if any #### START of preliminary checks ######### ##### END of preliminary checks ####### # Handle manual control parameters like start, stop, status, restart, etc. case "$1" in start) # Start daemons. echo -n $"Starting <myservice> daemon: " echo daemon <myservice> echo ;; stop) # Stop daemons. echo -n $"Shutting down <myservice>: " killproc <myservice> echo # Do clean-up works here like removing pid files from /var/run, etc. ;; status) status <myservice> ;; restart) $0 stop $0 start ;; *) echo $"Usage: $0 {start|stop|status|restart}" exit 1 esac exit 0 

把2条评论放到你的脚本中:

 # chkconfig: - 90 10 # description: description of your service 

以root身份运行:

 chkconfig --add my_service 

一个基本的Unix守护进程执行以下操作:

 fork close all filedescriptors (stdout,stderr, etc) chdir / signal handeling (sighup, sigterm etc) while do stuff sleep(xx) done 

(例如C:daemon.c)

关于如何安装启动脚本的红帽示例:

在系统启动时用redhat启动deamon,你需要一个init脚本。 它应该放在/etc/init.d中

初始化脚本示例:

码:

 # chkconfig: 3 99 1 # description: my daemon case "$1" in 'start') /usr/local/bin/mydaemon ;; 'stop') pkill mydaemon ;; 'restart') pkill -HUP mydaemon ;; esac 

第一行将告诉chkconfig以优先级99启动运行级别3的守护程序,并在服务器关闭时将其作为优先级1来终止。

要安装启动脚本,请使用以下命令:chkconfig –add ./scriptabove现在,它将在服务器启动时启动。

马上启动使用:服务启动

如果你想了解更多详细信息,请访问链接

希望这有些帮助!

不同的Linux发行版包括不同的服务管理工具。 你应该看看launchd , OpenRC (现在在Gentoo上)和SystemD (例如在Arch上)

希望这可以帮助 :)

chkconfig – 添加your_service_name