在Windows系统里,我们一般使用 services.msc 工具对服务进行管理;Linux系统,不论是Redhat系还是Debian系,都是使用 systemctl 工具。
# 查看服务状态
sudo systemctl status sshd
# 启动,停止,重启服务
sudo systemctl start sshd
sudo systemctl stop sshd
sudo systemctl restart sshd
# 设置,取消服务开机启动
sudo systemctl enable sshd
sudo systemctl disable sshd
# 查看系统中有哪些服务
sudo systemctl list-unit-files --type service拓展:自定义一个服务
服务配置文件保存在目录 /usr/lib/systemd/system/ 下,例如sshd服务的配置文件为: /usr/lib/systemd/system/sshd.service,用cat命令查看配置文件内容:
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target
# Migration for Fedora 38 change to remove group ownership for standard host keys
# See https://fedoraproject.org/wiki/Changes/SSHKeySignSuidBit
Wants=ssh-host-keys-migration.service
[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target里面定义了这个服务的基本信息,启动方式等等,仿照这个格式即可注册一个自定义的服务。