Linux服务新手命令教程(附实例)
在此页
- Linux 服务命令
- 如何使用服务命令?
- 结论
服务命令允许您运行 System V 初始化脚本。在本教程中,我们将使用一些易于理解的示例简要讨论此工具。但在我们这样做之前,值得一提的是,这里的所有示例都已经在 Ubuntu 18.04 LTS 机器上进行了测试。
Linux服务命令
以下是 Linux 中服务命令的可用语法:
service SCRIPT COMMAND [OPTIONS]
service --status-all
service --help | -h | --version
手册页是这样解释的:
service runs a System V init script or systemd unit in as predictable an environment as possible,
removing most environment variables and with the current working directory set to /.
The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT, or the name
of a systemd unit. The existence of a systemd unit of the same name as a script in /etc/init.d
will cause the unit to take precedence over the init.d script. The supported values of COMMAND
depend on the invoked script. service passes COMMAND and OPTIONS to the init script unmodified.
For systemd units, start, stop, status, and reload are passed through to their systemctl/initctl
equivalents.
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. The
status is [ + ] for running services, [ - ] for stopped services and [ ? ] for services
without a status command. This option only calls status for sysvinit jobs.
如何使用服务命令?
服务命令行工具的使用一点也不复杂。让我们谈谈在处理 httpd 和 sshd 等脚本时如何使用此实用程序。
以下是使用服务运行脚本的方式:
service httpd start
以下是如何使用服务停止已经运行的脚本:
service sshd stop
以下是获取脚本状态的方法:
service httpd status
继承人如何重新启动和已经运行的服务:
service sshd restart
结论
根据您作为 Linux 用户的权限,您可能希望使用 sudo 命令运行上述所有示例。要了解有关服务命令的更多信息,请访问其手册页。