`

我使用过的Linux命令之service - 系统服务管理

阅读更多

我使用过的Linux命令之service - 系统服务管理

本文链接:http://codingstandards.iteye.com/blog/985455   (转载请注明出处)

 

用途说明

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地址等信息时,经常需要把网络重启使之生效。

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

 

示例二 重启MySQL

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

 

示例三 service脚本源码展示

[root@web ~]# 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
[root@web ~]#

 

示例四 crond服务的源码

[root@web 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
[root@web 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

 

 

返回 我使用过的Linux命令系列总目录

 

1
1
分享到:
评论

相关推荐

    Linux教程,主要内容:Linux 命令、Linux 系统运维、软件运维、精选常用Shell脚本.zip

    linux教程: 查看 Linux 命令帮助信息 - 关键词:help, whatis, info, which, where...Linux 系统管理 - 关键词:reboot, exit, shutdown, date, mount, umount, ps, kill, systemctl, service, crontab Linux 网络管理

    Linux1-代理服务器--清除代理服务器上cache记录.doc

    Linux1 代理服务器 清除代理服务器上cache记录 作为管理员要定期对服务器的cache记录进行清除,这样才能保证用户访问服务器的 速度不受影响。cache(高速缓冲存储器)是一种特殊的存储器子系统,其中保存了频繁 使用...

    《Linux服务器配置与管理》教学课件—第-15-章--Samba-服务器配置.pptx

    如果用户希望系统在启动时就自动加载 Samba 服务,则可以使用如下命令设置开机 自启动: [root@master ~]# chkconfig smb on 注意:正在将请求转发到"systemctl enable smb.service"。 Created symlink from /etc/...

    Linux操作系统实验三.doc

    1、通过实验了解和熟悉Linux系统管理; 2、掌握用户和组管理命令; 3、掌握软件包安装命令和步骤; 4、掌握网络通信管理命令; 5、掌握进程管理命令; 6、掌握系统的服务管理命令; 7、掌握磁盘操作管理命令。 二、 开发...

    《Linux服务器配置与管理》教学课件—第-12-章--FTP-服务器配置.pptx

    第12章 FTP 服务器配置 《Linux服务器配置与管理》教学课件—第-12-章--FTP-服务器配置全文共33页,当前为第1页。 目录 12.1 12.2 12.3 FTP 服务器安装 FTP 服务器配置文件 任务实战 《Linux服务器配置与管理》教学...

    详解Linux 服务管理两种方式service和systemctl

    1.service命令 ...systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。 systemd对应的进程管理命令是systemctl 1)systemctl命令

    oracle启动与管理服务脚本

    此脚本是将安装好的oracle数据库注册为linux服务,让服务随机启动,同时便于对数据库的一些管理。...注意:如果数据库用户或者组有不同,需要修改脚本,本脚本未在集群环境下使用过,因此可能需要根据具体环境调整。

    Linux部署服务器常用命令.doc

    linux部署服务器常用命令 fdisk -l 查分区硬盘 df -h 查空间硬盘 cd / 进目录 ls/ll 文件列表 vi tt.txt i=insert 插入 shift+: 进命令行 wq 保存%退出 cat tt.txt 内容查看 pwd 当期目录信息 mkdir tt建目录 cp tt....

    Linux FTP服务搭建

    确认系统中vsFTPd的安装和版本情况: [root@localhost ~]#rpm –q vsftpd 安装vsFTPd命令: [root@localhost Server]#rpm –ivh vsftpd-2.0.5-12.el5.i386.rpm 在vsftpd.conf文件中配置匿名访问: anonymous_...

    linux用户创建

    service vsftpd start 启动Linux自带的FTP服务 2. 添加普通用户 useradd nagios –d /usr/local/nagios chown nagios.nagios /usr/local/nagios 将用户切换到nagios 用户,添加nagios的访问用户: su nagios ...

    嵌入式Linux程序设计案例与实验教程-实例代码

    实验1.1 熟悉Linux基本命令与文件目录系统2 1.3 全屏幕编辑器与vi6 1.3.1 vi简介6 1.3.2 基本命令7 1.3.3 常用操作7 实验1.2 全屏幕编辑器vi的使用7 1.4 Linux Shell编程8 1.4.1 Shell程序的编写和...

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

    对于广大Linux运维人员和系统管理人员来说,具有非常实用的指导意义。 全书共分五个篇幅,由14个章节组成,内容涉及Web应用、数据备份恢复、网络存储应用、性能优化与运维监控、集群高级应用方面,每个篇幅占用...

    ipvsadm命令 linux 虚拟服务器管理

    ipvsadm命令用于在Linux内核中设置,维护或检查虚拟服务器表。 Linux虚拟服务器可用于基于两个或更多节点的集群构建可伸缩网络服务。 群集的主节点将服务请求重定向到将实际执行服务的服务器主机集合。 支持的功能...

    Kiwi-Syslog日志服务器搭建.doc

    1. 安装要求: 系统:windows 2012 R2 Standerd 环境:安装.net3.5和web服务 软件版本:K + SolarWinds Event Log Forwarder for Windows 日志服务器IP: 2. 安装过程: 运行开始安装: 至此,Kiwi Syslog server...

    Linux系统中systemctl命令详解

    Linux Systemctl是一个系统管理守护进程、工具和库的集合,用于取代System V、service和chkconfig命令,初始进程主要负责控制systemd系统和服务管理器

    新版Android开发教程.rar

    � Android 更像一款桌面环境为 Java 的 Linux 操作系统。有助于 Google 实现其 " 随时随地为每个人提供信 息 " 的企业战略。 HTC HTC HTC HTC Dream/G1 Dream/G1 Dream/G1 Dream/G1 具体配置 硬件 3.17 英寸 HVGA ...

    Linux环境搭建

    /sbin:s就是Super User的意思,这里存放的是系统管理员使用的系统管理程序。 /selinux:这个目录是Redhat/CentOS所特有的目录,Selinux是一个安全机制,类似于windows的防火墙 /srv:service缩写,该目录存放...

    嵌入式Linux程序设计案例与实验教程(配套光盘)第一部分

    1.2 Linux系统的使用2 实验1.1 熟悉Linux基本命令与文件目录系统2 1.3 全屏幕编辑器与vi6 1.3.1 vi简介6 1.3.2 基本命令7 1.3.3 常用操作7 实验1.2 全屏幕编辑器vi的使用7 1.4 Linux Shell编程8 1.4.1 Shell...

    Linux chkconfig 命令的使用

    chkconfig 命令用于更新和查询系统服务的运行等级信息。它可查询操作系统在每一个运行等级中会自动执行哪些系统服务,包括各类常驻服务,比如 httpd、sshd、mysqld 等。 chkconfig 实际上是通过改变七个不同运行等级...

    嵌入式Linux程序设计案例与实验教程(配套光盘)第二部分

    1.2 Linux系统的使用2 实验1.1 熟悉Linux基本命令与文件目录系统2 1.3 全屏幕编辑器与vi6 1.3.1 vi简介6 1.3.2 基本命令7 1.3.3 常用操作7 实验1.2 全屏幕编辑器vi的使用7 1.4 Linux Shell编程8 1.4.1 Shell...

Global site tag (gtag.js) - Google Analytics