1.建立服务文件
系统服务脚本目录:/lib/systemd/system/
服务以.service
结尾
这边以kangle开机运行为例
vi /lib/systemd/system/kangle.service
输入 i 编辑,把:
[Unit]
Description=Kangle Web Service
After=network.target
[Service]
Type=forking
ExecStart=/vhs/kangle/bin/kangle
ExecReload=/vhs/kangle/bin/kangle --reboot
ExecStop=/vhs/kangle/bin/kangle -q
[Install]
WantedBy=multi-user.target
放进去,按 Esc 然后输入 :wq! 回车保存退出
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式附录:
Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务, 不要使用此类型启动, 除非该服务是socket激活型。
Type=forking:systemd认为当该服务进程fork, 且父进程退出后服务启动成功。对于常规的守护进程(daemon), 除非你确定此启动方式无法满足需求, 使用此类型启动即可。
使用此启动类型应同时指定 PIDFile=, 以便systemd能够跟踪服务的主进程。
Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。
Type=notify:与 Type=simple 相同, 但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。
Type=dbus:若以此方式启动, 当指定的 BusName 出现在DBus系统总线上时, systemd认为服务就绪。
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
User: 以用户执行
Group: 以用户组执行
WorkingDirectory: 服务工作目录
RestartSec: 重启服务延时
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]服务安装的相关设置,可设置为多用户
2.设置开机自启动
systemctl enable kangle.service
3.其他命令
使某服务自动启动systemctl enable kangle.service
使某服务不自动启动systemctl disable kangle.service
检查服务状态systemctl status kangle.service
(服务详细信息)systemctl is-active kangle.service
(仅显示是否 Active)
显示所有已启动的服务systemctl list-units --type=service
启动某服务systemctl start kangle.service
停止某服务systemctl stop kangle.service
重启某服务systemctl restart kangle.service
启动kangle服务
systemctl start kangle.service
设置开机自启动
systemctl enable kangle.service
停止开机自启动
systemctl disable kangle.service
查看服务当前状态
systemctl status kangle.service
重新启动服务
systemctl restart kangle.service
查看所有已启动的服务
systemctl list-units --type=service
如果更改了 .service文件则要刷新配置
systemctl daemon-reload
下面顺便写一下Debian8.11.1以上没rc.loacl,创建rc.loacl
由于比较新的Linux发行版已经没有rc.local文件了
1.建立服务文件rc-local.service
vi /lib/systemd/system/rc-local.service
输入 i 编辑,把(debian8.11.1提取的):
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
SysVStartPriority=99
放进去,按 Esc 然后输入 :wq! 回车保存退出
2.设置开机自启动
systemctl enable rc-local.service
3.建立服务文件rc-local
vi /etc/rc.local
输入 i 编辑,把(debian8.11.1提取的):
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
exit 0
放进去,按 Esc 然后输入 :wq! 回车保存退出
4.赋予执行权限
chmod +x /etc/rc.local
5.重启和检查状态
重启
reboot
检查状态
systemctl status rc-local.service
评论区(1条评论)
欢迎加入 Typecho 大家族