linux 添加服务开机启动,Linux添加service服务及开机启动

提问:

我自己开发了一个绑定端口的daemon程序,想加到centos的服务中,这样我就可以使用service start这样的命令启动了。请问,我该如何添加到service当中?

回答:

先写一个脚本, 这个脚本用来启动你的程序, 或者停止你的程序, 可参考如下

#! /bin/sh

# smartmontools init file for smartd

# Copyright (C) 2002-4 Bruce Allen 

# $Id: smartd.initd,v 1.2 2004/09/17 11:55:28 arjanv Exp $

# For RedHat and cousins:

# chkconfig: 2345 40 40

# description: Self Monitoring and Reporting Technology (SMART) Daemon

# processname: smartd

# This program is free software; you can redistribute it and/or modify it

# under the terms of the GNU General Public License as published by the Free

# Software Foundation; either version 2, or (at your option) any later

# version.

# You should have received a copy of the GNU General Public License (for

# example COPYING); if not, write to the Free Software Foundation, Inc., 675

# Mass Ave, Cambridge, MA 02139, USA.

# This code was originally developed as a Senior Thesis by Michael Cornwell

# at the Concurrent Systems Laboratory (now part of the Storage Systems

# Research Center), Jack Baskin School of Engineering, University of

# California, Santa Cruz. http://ssrc.soe.ucsc.edu/.

# Uncomment the line below to pass options to smartd on startup.

# Note that distribution specific configuration files like

# /etc/{default,sysconfig}/smartmontools might override these

#smartd_opts="--interval=1800"

SMARTD_BIN=/usr/sbin/smartd

# Source function library

. /etc/rc.d/init.d/functions

[ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools

[ ! -f /etc/smartd.conf ] && smartd-conf.py > /etc/smartd.conf

RETVAL=0

prog=smartd

case "$1" in

start)

echo -n $"Starting $prog: "

daemon $SMARTD_BIN $smartd_opts

touch /var/lock/subsys/smartd

echo

exit 0

;;

stop)

echo -n $"Shutting down $prog: "

killproc $SMARTD_BIN

rm -f /var/lock/subsys/smartd

echo

;;

reload)

echo -n $"Reloading $prog daemon configuration: "

killproc $SMARTD_BIN -HUP

RETVAL=$?

echo

;;

report)

echo -n $"Checking SMART devices now: "

killproc $SMARTD_BIN -USR1

RETVAL=$?

echo

;;

restart)

$0 stop

$0 start

;;

status)

status $prog

;;

*)

echo $"Usage: $0 {start|stop|reload|report|restart|status}"

RETVAL=1

esac

exit $RETVAL

这个服务是smartd, 你也可以参考/etc/rc.d/init.d目录下的脚本, 基本都差不多

2.将写好的脚本放入/etc/rc.d/init.d目录

3.用命令service xxx start/stop/restart就可以执行了

4.如果你想在开机的时候,就启动你的服务, 那么你需要对你的配置文件进行配置

见上面的例第7、8行:

# chkconfig: 2345 40 40

# description: Self Monitoring and Reporting Technology (SMART) Daemon

不要以为这一行市注释, 其实不尽然

如果执行#chkconfig service_name on/off

就会在/etc/rc.dx相应的目录中 添加/删 除启动关闭项, 用于在系统启动和关闭时的自动启动和关闭

这里将在/etc/rc.d2,3,4,5目录下 添加/删除, 后面的40 40分别是启动顺序和关闭顺序

也就意味着系统在处于2,3,4,5模式下时, 会启动你的程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux服务器上,我们可以通过服务service)的方式来控制开机启动服务是一种系统进程,会在Linux服务启动时自动启动,并且可以通过命令行终端进行管理。 Linux服务器上的服务通常位于 /etc/init.d 目录下,其中包含了大量的服务脚本。但是,并不是所有的服务都需要开机启动,我们可以通过 chkconfig 命令来查询和管理服务开机启动状态。 chkconfig 命令的语法为: chkconfig --level levels service_name [on/off] 其中 levels 表示运行级别,可以是 0-6 及 S ,0 表示关机、6 表示重启,S 表示启动service_name 表示服务名称。 使用 chkconfig 命令可以查询一个服务在所有运行级别下的状态: chkconfig --list service_name 使用 chkconfig 命令可以设置一个服务在某个运行级别下是否自启动: chkconfig --level 3 service_name on/off 使用 chkconfig 命令可以设置一个服务在所有运行级别下是否自启动: chkconfig --add service_name 使用上述命令后,该服务将被添加到 /etc/rc.d/rcN.d 目录中,其中 N 表示运行级别。 在Linux服务启动时,系统会读取 /etc/init.d 目录中的所有服务脚本,并根据运行级别决定哪些服务需要开机启动。如果该服务添加到了 /etc/rc.d/rcN.d 目录中,那么在启动或者停止Linux服务器时,该服务将自动启动或停止。 总之,通过 chkconfig 命令,我们可以轻松地实现Linux服务器上服务开机启动
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值