学习啦 > 学习电脑 > 操作系统 > Linux教程 > linux的service命令

linux的service命令

时间: 佳洲1085 分享

linux的service命令

  linux下的service命令主要是对系统服务进行管理,下面由学习啦小编为大家整理了linux的service命令的相关知识,希望对大家有帮助!

  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

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

  linux的service命令使用示例

  示例一 网络重启

  当修改了主机名、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]#

3632022