Linux-Service服务

用途说明

service命令用于对系统服务进行管理,比如启动(start)、停止(stop)、重启(restart)、查看状态(status)等。相
关的命令还包括chkconfig、ntsysv等,chkconfig用于查看、设置服务的运行级别,ntsysv用于直观方便的设置各个服务是否自动
启动。service命令本身是一个shell脚本,它在/etc/init.d/目录查找指定的服务脚本,然后调用该服务脚本来完成任务。

看看下面的手册页可能更加清楚的了解service的内幕:service运行指定服务(称之为System
V初始脚本)时,把大部分环境变量去掉了,只保留LANG和TERM两个环境变量,并且把当前路径置为/,也就是说是在一个可以预测的非常干净的环境中运
行服务脚本。这种脚本保存在/etc/init.d目录中,它至少要支持start和stop命令。

man service 写道

service(8)                                                          service(8)

NAME
      service - run a System V init script

SYNOPSIS
      service SCRIPT COMMAND [OPTIONS]

service --status-all

service --help | -h | --version

DESCRIPTION
      service  runs a System V init script in as predictable environment as possible, removing most environment vari-
      ables and with current working directory set to /.

The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT.  The supported values  of
      COMMAND depend on the invoked script, service passes COMMAND and OPTIONS it to the init script unmodified.  All
      scripts should support at least the start and stop commands.  As a special case, if COMMAND is  --full-restart,
      the script is run twice, first with the stop command, then with the start command.

service --status-all runs all init scripts, in alphabetical order, with the status command.

FILES
      /etc/init.d
             The directory containing System V init scripts.

ENVIRONMENT
      LANG, TERM
             The only environment variables passed to the init scripts.

SEE ALSO
      chkconfig(8), ntsysv(8)

Jan 2006                         service(8)

常用方式

格式:service <service>

打印指定服务<service>的命令行使用帮助。

格式:service <service> start

启动指定的系统服务<service>

格式:service <service> stop

停止指定的系统服务<service>

格式:service <service> restart

重新启动指定的系统服务<service>,即先停止(stop),然后再启动(start)。

格式:chkconfig --list

查看系统服务列表,以及每个服务的运行级别。

格式:chkconfig <service> on

设置指定服务<service>开机时自动启动。

格式:chkconfig <service> off

设置指定服务<service>开机时不自动启动。

格式:ntsysv

以全屏幕文本界面设置服务开机时是否自动启动。

使用示例

示例一 网络重启

当修改了主机名、ip地址等信息时,经常需要把网络重启使之生效。

[[email protected] root]# service network
用法:/etc/init.d/network {start|stop|restart|reload|status}
[[email protected] root]# service network status
配置设备:
lo eth0
当前的活跃设备:
lo eth0
[[email protected] root]# service network restart
正在关闭接口 eth0:                                        [  确定  ]
关闭环回接口:                                             [  确定  ]
设置网络参数:                                             [  确定  ]
弹出环回接口:                                             [  确定  ]
弹出界面 eth0:                                            [  确定  ]
[[email protected] root]#

示例二 重启MySQL

[[email protected] root]# service mysql
mysql: unrecognized service
[[email protected] root]# service mysqld
用法:/etc/init.d/mysqld {start|stop|status|condrestart|restart}
[[email protected] root]# service mysqld status
mysqld (pid 1638) 正在运行...
[[email protected] root]# service mysqld restart
停止 MySQL:                                               [  确定  ]
启动 MySQL:                                               [  确定  ]
[[email protected] root]#

示例三 service脚本源码展示

[[email protected] ~]# cat /sbin/service
#!/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
[[email protected] ~]#

示例四 crond服务的源码

[[email protected] init.d]# cat /etc/init.d/crond
#! /bin/bash
#
# crond          Start/Stop the cron clock daemon.
#
# chkconfig: 2345 90 60
# description: cron is a standard UNIX program that runs user-specified \
#              programs at periodic scheduled times. vixie cron adds a \
#              number of features to the basic UNIX cron, including better \
#              security and more powerful configuration options.
# processname: crond
# config: /etc/crontab
# pidfile: /var/run/crond.pid

# Source function library.
. /etc/init.d/functions
. /etc/sysconfig/crond
t=${CRON_VALIDATE_MAILRCPTS:-UNSET}
[ "$t" != "UNSET" ] && export CRON_VALIDATE_MAILRCPTS="$t"
 
# See how we were called.
 
prog="crond"

start() {
        echo -n $"Starting $prog: "
        if [ -e /var/lock/subsys/crond ]; then
            if [ -e /var/run/crond.pid ] && [ -e /proc/`cat /var/run/crond.pid` ]; then
                echo -n $"cannot start crond: crond is already running.";
                failure $"cannot start crond: crond already running.";
                echo
                return 1
            fi
        fi
        daemon crond $CRONDARGS
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/crond;
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        if [ ! -e /var/lock/subsys/crond ]; then
            echo -n $"cannot stop crond: crond is not running."
            failure $"cannot stop crond: crond is not running."
            echo
            return 1;
        fi
        killproc crond
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/crond;
        return $RETVAL
}

rhstatus() {
        status crond
}

restart() {
        stop
        start
}

reload() {
        echo -n $"Reloading cron daemon configuration: "
        killproc crond -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  status)
        rhstatus
        ;;
  condrestart)
        [ -f /var/lock/subsys/crond ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|reload|restart|condrestart}"
        exit 1
esac
[[email protected] init.d]#

问题思考

相关资料

【1】测试人生 linux 中不常用的命令--service
http://www.51testing.com/?uid-66775-action-viewspace-itemid-78574
【2】linux大棚 《service》-“linux命令五分钟系列”之二
http://roclinux.cn/?p=47
【3】yqh860921 Linux Service 服务管理
http://blogold.chinaunix.net/u3/95470/showart_1934759.html
【4】酷勤 Linux system service 注释
http://www.kuqin.com/linux/20090824/67321.html
【5】momodog 自定义Linux Service
http://momodog.iteye.com/blog/286047

时间: 2024-08-28 21:27:26

Linux-Service服务的相关文章

将java打jar包成linux后台服务service

将java打jar包成linux后台服务service 第一步:将java程序打成jar包 build.gradle配置文件中加spring-boot-gradle-plugin插件,具体配置如下(配置完成后刷新gradle项目) plugins {   id 'org.springframework.boot' version '1.5.4.RELEASE' } springBoot {   executable = true } 打包,在build.gradle所在的目录下,运行如下命令 g

Linux网络服务之HTTP(1)

Linux网络服务之HTTP(1) 实验要求: 1.主机名设为:www.zhy.com,默认首页包括:index.html.index.php,开启保持连接,确认默认httpd是否支持php 2.只允许192.168.1.1访问www.zhy.com,允许所有用户访问www.zhy.com/user/index.html 3.客户端访问/var/www/html/admin/需要输入用户名密码验证 4.客户端访问http://www.zhy.com/bbs时可以访问/var/www/html/u

linux开机服务自启

有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务,主要用三种方式进行这一操作: ln -s             在/etc/rc.d/rc*.d目录中建立/etc/init.d/服务的软链接(*代表0-6七个运行级别之一) chkonfig          命令行运行级别设置 ntsysv            伪图形运行级别设置 注意: 这三种方式主要用于以redhat为基础的发行版 如果还不知道运行级别是什么,那么最好先看看相关资料再实验 第一种方式:ln -s 建立

初学linux网络服务之vsftp服务实验

实验拓扑: Linux Client -----RHEL5.9(vmnet1)----------(vmnet1) Win7 Client 实验一:测试默认安装vsftpd的结果 匿名用户与本地用户都可以登录 匿名用户登录到/var/ftp,只能下载不能上传 本地用户登录到本地用户的家目录,可以上传和下载 服务器端设置 [[email protected] ~]# cd /misc/cd/Server                 //进入RHEL5.9光盘 [[email protected

初学linux网络服务之samba服务实验

实验拓扑: Linux Client -----RHEL5.9(vmnet1)----------(vmnet1) Win7 Client 实验一:Samba匿名共享 工作组为Tarena 将目录 /usr/src 共享给所有人 共享名设为 tools 允许所有人访问.无需密码验证 访问权限为只读 1.安装软件包 [[email protected] ~]# rpm -q samba-client samba samba-common  //查看安装包 package samba-client

windows无法启动VMware Authorization Service服务

VMware也用了一年了,一直好好的,今天突然出现启动不了VMware里的linux系统了, Authorization  Service问题,在windows系统里查看服务,找到VMware Authorization Service,点启动,弹出无法启动VMware Authorization  Service  1075服务不存在. 上网找到答案:这是装了VMware虚拟机软件之后,又用了系统优化工作之后造成的 万恶的金山毒霸,果断卸载了 windows无法启动VMware Authori

初学linux网络服务之HTTP服务实验

实验拓扑: Linux Client -----RHEL5.9(vmnet1)----------(vmnet1) Win7 Client 实验一:查看默认HTTP配置 找到默认红帽欢迎页面 (/etc/httpd/conf/httpd.conf ---->Include ----> /etc/httpd/conf.d  ----> welcome.conf  ----> /var/www/error/noindex.html) 前提条件: 1.配置IP [[email prote

Linux网络服务之FTP

Linux网络服务之FTP 实验要求: 1.配置可匿名上传FTP服务.(此需求为纯实验需求,实际环境中一般不使用此功能) 2.配置本地用户访问FTP服务,拒绝匿名用户访问,验证黑白名单,禁锢普通用户nick在自己的主目录里面. 3.更改匿名用户的站点为/ftp/ftp,更改本地用户的站点为/ftp/user,设置匿名用户下载速率50KB/s,本地用户100KB/s,最多20个并发,每IP地址最多2个并发. 实验步骤: 1.安装软件包 搭建FTP服务需要安装vsftpd软件包,使用yum方式安装(

CentOS: Make Command not Found and linux xinetd 服务不能启动

在centos 安装编译器 yum -y install gcc automake autoconf libtool make linux xinetd 服务不能启动: [[email protected] xinetd.d]# service xinetd restart xinetd: unrecognized service 安装xinetd 包 [[email protected] ~]# yum -y install xinetd  rpm包安装成功 [[email protected

linux 的服务与进程管理(二)

2.linux 的服务与进程管理 [2.1]系统启动流程 简单的介绍下linux的系统启动流程,方便我们深入了解linux操作系统,对排除linux系统故障进行帮助.启动流程虽然简单但背后还有着更加复杂的底层函数调用,等待咱们去研究,本节课就算抛砖引玉了. 启动第一步--加载BIOS 当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘信息.内存信息.时钟信息.PnP特性等等