`
脸同学
  • 浏览: 38726 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

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

阅读更多

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 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/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模式下时, 会启动你的程序

不说得是否清楚

分享到:
评论

相关推荐

    IIS6.0 IIS,互联网信息服务

     1、插入windows xp安装光盘,打开控制面板,然后打开其中的“添加/删除程序” 2、在添加或删除程序窗口左边点击“添加/删除Windows组建”[1] 3、捎带片刻系统会启动Windows组建向导,在Internet信息服务(IIS)...

    集群好书《高性能Linux服务器构建实战》 试读章节下载

    5.6.2 添加密钥到授权密钥文件中 5.7 unison的使用 5.7.1 本地使用unison 5.7.2 远程使用unison 5.7.3 unison参数说明 5.7.4 通过配置文件来使用unison 5.8 本章小结 第6章 ext3文件系统反删除利器...

    windows实用dos命令大全

    (1)子目录在删除前必须是空的,也就是说需要先进入该子目录,使用DEL(删除文件的命令)将其子目录下的文件删空,然后再退回到上一级目录,用RD命令删除该了目录本身;  (2)不能删除根目录和当前目录。  例:...

    超级有影响力霸气的Java面试题大全文档

    Stateful Session Bean 与 Stateless Session Bean ,这两种的 Session Bean都可以将系统逻辑放在 method之中执行,不同的是 Stateful Session Bean 可以记录呼叫者的状态,因此通常来说,一个使用者会有一个相对应...

    adb1.0.26包含fastboot.exe

    一般能在「设置」-「关于手机」-「状态信息」-「IP地址」找到,也可以使用下文里 查看设备信息 - IP 地址 一节里的方法用 adb 命令来查看。 通过 IP 地址连接设备。 adb connect 这里的 &lt;device-ip-address&gt; 就是...

    java 面试题 总结

    Stateful Session Bean 与 Stateless Session Bean ,这两种的 Session Bean都可以将系统逻辑放在 method之中执行,不同的是 Stateful Session Bean 可以记录呼叫者的状态,因此通常来说,一个使用者会有一个相对应...

    数据库.txt

    --启动mysql服务与停止mysql服务命令: net start mysql net stop mysql --登陆与退出命令: mysql -h 服务器IP -P 端口号 -u 用户名 -p 密码 --mysql -h 127.0.0.1 -P 3306 -u root -p 123 --配置文件 /* ...

    第一章数据库的简介.docx

    --启动mysql服务与停止mysql服务命令: net start mysql net stop mysql --登陆与退出命令: mysql -h 服务器IP -P 端口号 -u 用户名 -p 密码 --mysql -h 127.0.0.1 -P 3306 -u root -p 123 --配置文件 /* ...

    Eclipse开发分布式商城系统+完整视频代码及文档

    │ 11.nginx的安装及启动、停止.avi │ 12.nginx的配置文件-通过端口号区分虚拟机.avi │ 13.通过域名配置虚拟机.avi │ 淘淘商城第二天笔记.docx │ ├─03.第三天 │ 01.课程回顾.avi │ 02.课程计划.avi │ 03....

Global site tag (gtag.js) - Google Analytics