怎么将自己写的程序添加到linux服务中,用service命令来启动或停止

怎么将自己写的程序添加到linux服务中,用service命令来启动或停止,第1张

Linux下添加服务流程:

按规则编写服务脚本

将编写的脚本放到/etc/init.d/,将myserviced的访问权限加上“可执行”:chmod +x myserviced

增加服务:chkconfig --add myserviced

启停服务:service myserviced start

service myserviced stop

完成

你把我下面给你的脚本复制下,然后定名为service,放到/sbin/下

#!/bin/sh

. /etc/init.d/functions

VERSION="`basename $0` ver. 0.91"

USAGE="Usage: `basename $0` <option >| --status-all | \

[ service_name [ command | --full-restart ] ]"

SERVICE=

SERVICEDIR="/etc/init.d"

OPTIONS=

if [ $# -eq 0 ]then

echo "${USAGE}" >&2

exit 1

fi

cd /

while [ $# -gt 0 ]do

case "${1}" in

--help | -h | --h* )

echo "${USAGE}" >&2

exit 0

--version | -V )

echo "${VERSION}" >&2

exit 0

*)

if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]then

cd ${SERVICEDIR}

for SERVICE in * do

case "${SERVICE}" in

functions | halt | killall | single| linuxconf| kudzu)

*)

if ! is_ignored_file "${SERVICE}" \

&&[ -x "${SERVICEDIR}/${SERVICE}" ]then

env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" status

fi

esac

done

exit 0

elif [ $# -eq 2 -a "${2}" = "--full-restart" ]then

SERVICE="${1}"

if [ -x "${SERVICEDIR}/${SERVICE}" ]then

env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" stop

env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" start

exit $?

fi

elif [ -z "${SERVICE}" ]then

SERVICE="${1}"

else

OPTIONS="${OPTIONS} ${1}"

fi

shift

esac

done

if [ -x "${SERVICEDIR}/${SERVICE}" ]then

env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" ${OPTIONS}

else

echo $"${SERVICE}: unrecognized service" >&2

exit 1

fi

其实service就是一个脚本,这是我linux上的。你还可以把service名改成abc等其他的名字,服务就可以 abc 服务 start|stop|status 了,嘿嘿...

当然也可能直接用不了,那你就需要加上/sbin/service了,或者你在/etc/profile中加个变量

export PATH=/sbin/:/usr/sbin/就可以了 记得让变量生效啊 source /etc/profile

如果有错,自行更改,我英文不好...

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

--------------------

#! /bin/sh

# smartmontools init file for smartd

# Copyright (C) 2002-4 Bruce Allen <smartmontools-support@lists.sourceforge.net>

# $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 softwareyou can redistribute it and/or modify it

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

# Software Foundationeither 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/init.d目录下的脚本, 基本都差不多

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

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

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

见上面的例:

# chkconfig: 2345 40 40

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

如果执行#chkconfig service_name on/off

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

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

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


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11641662.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存