用途说明

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命令中service的用法的更多相关文章

  1. Linux命令中特殊符号

    转自:http://blog.chinaunix.net/uid-16946891-id-5088144.html   在shell中常用的特殊符号罗列如下:# ; ;; . , / \ 'strin ...

  2. [转帖]Linux命令中特殊符号

    Linux命令中特殊符号 转自:http://blog.chinaunix.net/uid-16946891-id-5088144.html   在shell中常用的特殊符号罗列如下:# ; ;; . ...

  3. Linux命令中:rsync和cp之间的区别

    rsync:只拷贝那些更新的文件: cp -u:也可以实现类似效果: 两者都基本可以满足备份的需求: 只是一般情况下,用rsync做这类备份之类的事情,更多见: 在备份的操作中,拷贝,过期文件的删除是 ...

  4. Linux命令中使用正则表达式

    在使用grep.awk和sed命令时,需要使用正则表达式.比如我通过grep找代码编译结果中是否有错误.或者是否有我代码的错误.这里说下正则表达式基本的应用: • 匹配行首与行尾.• 匹配数据集.• ...

  5. linux命令中的“<”和“|”是什么意思?

    ”<” 表示的是输入重定向的意思,就是把<后面跟的文件取代键盘作为新的输入设备.”| ”则表示一个管道的意思,可以理解为东西从管道的一边流向另外一边.   cat file.json | ...

  6. Linux命令中dd的作用

    Linux命令中用vi编辑文本时dd的作用是删除整行文本的信息.

  7. Linux 命令中 more、less、head、tail 命令的用法

    more 命令 more 命令,功能类似 cat ,cat 命令是将整个文件的内容从上到下显示在屏幕上. more 命令会一页一页的显示,方便使用者逐页阅读,而最基本的指令就是按空白键(space)往 ...

  8. Linux 命令中 find 和 xargs 命令的用法

    find 命令(一) find 命令主要作用是沿着文件层次结构向下遍历,匹配符合条件的文件,并执行相应的操作.Linux 下 find 命令提供了相当多的查找条件,功能很强大,对应的学习难度也比较大. ...

  9. Linux 命令中 which、whereis、locate 命令的用法

    which 命令 which 命令的作用是,在 PATH 变量指定的路径中搜索可执行文件的所在位置.它一般用来确认系统中是否安装了指定的软件. (1)命令格式 which 可执行文件名称 wherei ...

随机推荐

  1. c# datagridview导出Excel文件 问题

    今天vs2010c#开发做datagridview导出Excel文件时,发现一个问题,和大家探讨一下: 第一种方式:写流的方式 private void button_Excel_Click(obje ...

  2. 01GitLab的使用——创建项目并上传到GitLab

    借鉴:https://jingyan.baidu.com/article/9c69d48fe68cce13c9024e9c.html 登录GitLab网站,创建一个项目上传地址:https://blo ...

  3. elasticsearch regexp查询特殊字符处理

    regexp表面意思就是正则查询,但是如果遇到,查询条件中包含特殊的字符串, 就会发现,需要进行相应的转义处理 需要处理Lucene regexps即可: /** * 转义字符串中的特殊字符 * 仅过 ...

  4. django framework插件类视图方法

    1.使用类视图APIView重写API 类视图APIView,取代@api_view装饰器,代码如下: from rest_framework import status from rest_fram ...

  5. eclipse自定义自动补全语句

    1. Windows-->preferences 2. 弹出框选择, Java-->Editor-->Templates-->New 3. 弹出框输入, 1.Name--名字, ...

  6. [转]【会话技术】Cookie技术

    建立时间:6.29 & 6.30 一.会话技术简介 1.存储客户端的状态 由一个问题引出今天的内容,例如网站的购物系统,用户将购买的商品信息存储到哪  里?因为Http协议是无状态的,也就是说 ...

  7. 注意力机制---Attention、local Attention、self Attention、Hierarchical attention

    一.编码-解码架构 目的:解决语音识别.机器翻译.知识问答等输出输入序列长度不相等的任务. C是输入的一个表达(representation),包含了输入序列的有效信息. 它可能是一个向量,也可能是一 ...

  8. Discuz!开发之模板标签语法学习

    一.加载模板 使用template()函数显示已存在模板: 在Discuz!程序执行中可以通过 include template('模板文件夹/模板名称无后缀');的方式进行解析! template( ...

  9. woocommerce分类页产品数量修改

    我们用woocommerce建商城,不同的模板分类页产品数量不同,如果想要显示更多或更少的数量要如何修改呢?很简单,一行代码就能搞定!打开当前主题的function.php文件,加入如下代码,把18改 ...

  10. 每天一道Rust-LeetCode(2019-06-01)

    每天一道Rust-LeetCode(2019-06-01) 坚持每天一道题,刷题学习Rust. 题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的, ...