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命令。
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 - 系统服务管理(转)的更多相关文章
- Linux 命令 - service: 系统服务管理
命令格式 service SCRIPT COMMAND [OPTIONS] service --status-all service --help | -h | --version 实例 a) 查看 ...
- linux命令之进程管理命令
1.ps:查看进程 该命令用于列出命令执行时刻的进程快照,如果想要动态的显示进程信息,可以使用top命令. 参数 说明 a(常用) 显示与终端相关的所有进程,包含每个进程的完整路径 x(常用) 显示与 ...
- Linux命令整理,用户管理,用户组管理,系统管理,目录管理常用命令
知识点梳理 Linux课堂笔记 学习目标 能够知道什么是Linux系统以及它的应用场景 能够独立完成安装VMware虚拟机和网络配置 能够独立完成安装CentOS以及远程终端SecureCRT 能够熟 ...
- systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。
1.centos 检查服务是否开机自启 (ntpd是原生的服务,mysql是注册的服务) 参考:1.http://man.linuxde.net/systemctl
- linux下的系统服务管理及日志管理
1.ntsysv服务配置工具 用来配置哪些服务开启或关闭,图形界面,使用键盘来操作. 安装ntsysv服务的命令:yum install -y ntsysv 直接运行命令ntsysv 弹出配置界面: ...
- Linux命令(用户管理、组和时间管理)
用户管理 Linux系统是一个多用用户的系统 用户分为三类: 超级用户(root)用户的id是0 伪用户 用户的id是1----499,虽然存在,但不能被登录 ...
- 【linux命令】权限管理命令(chattr、lsattr、sudo)
目录 chattr lsattr sudo 一.chattr命令 chattr命令用来修改文件系统的权限属性,只有 root 用户可以使用,建立凌驾于 rwx 基础权限之上的授权. PS:chattr ...
- Linux 命令整理 —— 用户管理
Linux用户管理以读.写.执行动作为权限,以用户组为单位,限制用户行为.对于文件的的操作,可以限制读.写.执行中的哪一种,也可以限制文件所有者.组用户.组外用户相应的权限. 所以,要建立用户,最好先 ...
- Linux命令--用户用户组管理
新增用户组 : groupadd groupadd [-g GID] 组名 不加-g 则按照系统默认的gid创建组,跟用户一样,gid也是从500开始的 修改用户组信息 : groupmod grou ...
随机推荐
- 创建第一个MySQL数据库earth及表area
Windows 10家庭中文版,MySQL 5.7.20 for Win 64,2018-05-08 数据库earth描述: 用于记录地球上的事物,一期包含地理区域信息——表area. 字符集编码:u ...
- 洛谷P3621风铃
传送门啦 分析: 这个题看起来像是个树形dp,嗯,就是看起来像. 所以我们就按树形dp的思路去分析就好了,这个题是一个树形dp的变形题. 和以前建树是一样的,我们用邻接表来进行储存.利用邻接表的特性, ...
- MySQL基础 - 权限配置
为数据库创建特定的用户和密码 mysql>grant all privileges on <database>.* to '<username>'@'localhost' ...
- Luogu P2069 【松鼠吃果子】
推荐一波数组模拟链表的讲解 这道题呢,数组写的话不好删除(因为后面要接过来),自然想到链表 对于一个果子,我们可以维护其前驱和后继,我们不妨记与一个点相邻的上面的点为其前驱,下面的点为其后继 观察到题 ...
- UVA10212 【The Last Non-zero Digit.】
暴力可做!!!(十秒还不打暴力!!!)暴力算阶乘边算边取余上代码 #include<iostream> #define int long long //开long long using n ...
- 四B象限图
- 41-2:和为S的连续正数序列
import java.util.ArrayList; /** * 面试题41-题目2:和为S的连续正数序列 * 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案 ...
- CodeForces - 620C Pearls in a Row 贪心 STL
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- TPS和QPS是什么,他们的区别是什么
一.TPS:Transactions Per Second(每秒传输的事物处理个数),即服务器每秒处理的事务数.TPS包括一条消息入和一条消息出,加上一次用户数据库访问.(业务TPS = CAPS × ...
- Swift2.0语言教程之类的嵌套与可选链接
Swift2.0语言教程之类的嵌套与可选链接 Swift2.0语言类的嵌套 在一个类中可以嵌套一个或者多个类.它们的嵌套形式也是不同的,大致分为了两种:直接嵌套和多次嵌套.下面依次讲解这两种方式. S ...