service命令用于对系统服务进行管理,比如启动(start)、停止(stop)、重启(restart)、查看状态(status)等。相关的命令还包括chkconfig、ntsysv等,chkconfig用于查看、设置服务的运行级别,ntsysv用于直观方便的设置各个服务是否自动启动。service命令本身是一个shell脚本,它在/etc/init.d/目录查找指定的服务脚本,然后调用该服务脚本来完成任务。
1.命令格式:
service SCRIPT COMMAND [OPTIONS]
service [服务] [操作] [选项]
2.命令功能:
service命令用于启动、停止、重新启动和关闭系统服务,还可以显示所有系统服务的当前状态。
3.常用参数:
1) status
2) start
3) stop
4) reload
5) disable
6) force-reload
4.常用方式:
格式: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
说明:以全屏幕文本界面设置服务开机时是否自动启动。
5.使用实例
实例1:网络重启
当修改了主机名、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]#
实例2:查看网络连接状态
命令:service network status
输出:
[root@localhost sunjimeng]# service network status
Configured devices:
lo eno16777736 配置_1
Currently active devices:
lo eno16777736
实例3:重启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]#
实例4:查看service命令的简介
命令:service mysql
输出:
[xiong@localhost ~]$ service mysql
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
实例5:查看指定服务的状态信息
命令:service xinetd status
输出:
[xiong@localhost ~]$ service xinetd status Redirecting to /bin/systemctl status xinetd.service xinetd.service - Xinetd A Powerful Replacement For Inetd Loaded: loaded (/usr/lib/systemd/system/xinetd.service; enabled) Active: active (running) since 日 2017-10-01 13:29:21 PDT; 22min ago Process: 1395 ExecStart=/usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid $EXTRAOPTIONS (code=exited, status=0/SUCCESS) Main PID: 1426 (xinetd) CGroup: /system.slice/xinetd.service └─1426 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid
实例6:停止xinetd服务
命令:service xinetd stop
输出:
[xiong@localhost ~]$ service xinetd stop Redirecting to /bin/systemctl stop xinetd.service Failed to issue method call: Access denied //没有root权限,所以拒绝访问 [xiong@localhost ~]$ su root 密码: //登入root [root@localhost sunjimeng]# service xinetd stop Redirecting to /bin/systemctl stop xinetd.service [root@localhost sunjimeng]# service xinetd status Redirecting to /bin/systemctl status xinetd.service xinetd.service - Xinetd A Powerful Replacement For Inetd Loaded: loaded (/usr/lib/systemd/system/xinetd.service; enabled) Active: inactive (dead) since 一 2016-06-20 00:15:27 PDT; 15s ago Process: 1395 ExecStart=/usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid $EXTRAOPTIONS (code=exited, status=0/SUCCESS) Main PID: 1426 (code=exited, status=0/SUCCESS) 6月 19 23:49:21 localhost.localdomain xinetd[1426]: removing echo 6月 19 23:49:21 localhost.localdomain xinetd[1426]: removing tcpmux 6月 19 23:49:21 localhost.localdomain xinetd[1426]: removing time 6月 19 23:49:21 localhost.localdomain xinetd[1426]: removing time 6月 19 23:49:21 localhost.localdomain xinetd[1426]: xinetd Version 2.3.15 st... 6月 19 23:49:21 localhost.localdomain xinetd[1426]: Started working: 0 avail... 6月 20 00:07:22 localhost.localdomain systemd[1]: Started Xinetd A Powerful ... 6月 20 00:08:07 localhost.localdomain systemd[1]: Started Xinetd A Powerful ... 6月 20 00:15:27 localhost.localdomain systemd[1]: Stopping Xinetd A Powerful... 6月 20 00:15:27 localhost.localdomain systemd[1]: Stopped Xinetd A Powerful ... Hint: Some lines were ellipsized, use -l to show in full.
实例7:重启守护进程
命令:service xinetd restart
输出:
[root@localhost sunjimeng]# service xinetd restart
Redirecting to /bin/systemctl restart xinetd.service
实例8:重新加载守护进程xinetd的配置文件
命令:service xinetd reload
输出:
[root@localhost sunjimeng]# service xinetd reload
Redirecting to /bin/systemctl reload xinetd.service
6.其他:
1.service程序与一般的程序的区别:
service(也称为daemon)表示后台运行的程序,一般随系统的启动自动地启动且在用户logoff后仍然能够继续运行。该daemon进程一般在启动后需要与父进程断开关系,并使进程没有控制终端(tty)。
因为daemon程序在后台执行,不需要于终端交互,通常就关闭STDIN、STDOUT和STDER。daemon无法输出信息,可以使用syslog或自己的日志系统进行日志处理。
可以使用/etc/rc.d/init.d/functions脚本中的daemon函数来将一般的程序启动为daemon:
[root@localhost sunjimeng]# ls /etc/rc.d/init.d/functions /etc/rc.d/init.d/functions
2.xinetd:
xinetd本身是一个service,他的作用是监听所有的端口,根据配置对不同的端口启动不同的应用。 对于有些需要在后台运行的程序,可以选择设置为service在后台一直运行,也可以选择使用xinetd来配置此程序根据需要激活。
对于需要频繁访问的服务,需要在/etc/rc.d/init.d下配置为service;对于不是频繁访问的服务,可以使用xinetd来激活,从而节约服务器的资源;总之service与xinetd,选一即可。
3.service命令和chkconfig命令与服务程序的关系:
service的管理工具是: /sbin/service
service的自动启动控制工具是: /sbin/chkconfig