Linux服务并设置开机自启动

目录


Linux服务器环境部署专栏目录(点击进入…)


Linux服务并设置开机自启动

通过制作Linux服务的方式启动,并设置开机启动。


(1)制作服务

在/etc/systemd/system/路径下创建redis.service文件

vim /etc/systemd/system/redis.service

写入如下内容:

[Unit]
Description=redis - start up automatically !
After=network.target

[Service]
Type=forking
ExecStart=cd /home/environment/redis-6.2.7 && ./redis-server ./conf/redis.conf
ExecReload=cd /home/environment/redis-6.2.7 && ./redis-server -s reload ./conf/redis.conf
ExecStop=cd /home/environment/redis-6.2.7 && ./redis-server -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存并退出 -> :wq


使用/bin/bash -c 命令
/bin/bash -c 命令表示后面的参数将会作为字符串读入作为执行的命令

[Unit]
Description=Redis - 自启动 !
After=network.target

[Service]
Type=forking
ExecStart=/bin/bash -c 'cd /home/environment/redis-6.2.7 && ./redis-server ./6379/redis.conf'
ExecStop=/bin/bash -c 'cd /home/environment/redis-6.2.7 && ./redis-cli -p 6379 -a 123456 shutdown'
ExecReload=/bin/bash -c 'cd /home/environment/redis-6.2.7 && ./redis-server -s reload ./6379/redis.conf'
PrivateTmp=true

[Install]
WantedBy=multi-user.target

修改权限

chmod +x redis.service

[Unit] -> 服务的说明

服务说明
Description对该服务的描述
Documention说明文档
Before在a.service服务启动前,启动本服务
After在b.target服务组启动后,再启动本服务
Wants弱依赖于c.service,即使被依赖服务启动失败或停止,本服务仍然运行
Requires强依赖于d.service,如果被依赖服务启动失败或停止,本服务也会停止

[Service] -> 服务运行参数的设置

多数配置项可不配置,ExecStart一般要自行设置

服务说明
EnvironmentFile服务的参数文件,形成$OPTIONS
Type服务启动类型。默认simple表示ExecStart为主进程
simple:默认值,执行ExecStart指定的命令,启动主进程
forking:(后台运行)以 fork 方式从父进程创建子进程,创建后父进程会立即退出
oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
dbus:当前服务通过D-Bus启动
notify:当前服务启动完毕,会通知Systemd,再继续往下执行
idle:若有其他任务执行完毕,当前服务才会运行
KillMode服务停止类型,默认control-group停止时杀死所有子进程,process只杀主进程,none只停止服务,不杀进程
ExecStart,ExecStop启动命令组,分别是服务启动时,停止时,重启时,启动前,启动后,停止后执行的命令
Restart服务重启类型,默认no不重启,on-success正常退出时重启,on-failure非正常退出时重启,还有always,on-abnormal,on-abort等
ExecReload重载命令
RestartSec间隔多久重启服务
比如: 某次异常后,等待5(s)再进行启动,默认值0.1(s)
StartLimitInterval重启次数。默认是10秒内如果重启超过5次则不再重启,设置为0表示不限次数重启
PrivateTmp服务分配独立的临时空间

RestartSec=5: : 无限次重启,

[Install] -> 运行级别下服务安装的相关设置

可设置为多用户,即系统运行级别为3;即把服务放在哪个服务组

服务说明
WantedBymulti-user.target

multi-user.target服务组中的服务,在systemctl enable xxx.service后,符号链接被放在/etc/systemd/system/multi-user.target.wants/目录下。而系统默认启动的Target可由systemctl get-defaults得到:

systemctl get-defaults

graphical.target
graphical.target又以multi-user.target为Requires和After关系,因此开机将自启动multi-user.target服务组;此外,.target文件用于配置服务组,但只有Unit字段

注意:[Service] 的启动、重启、停止命令全部要求使用绝对路径


(2)设置开机自启动

命令描述
systemctl daemon-reload重新加载所有的service服务
systemctl enable [service]设置开机启动
systemctl disable [service]取消开机启动
systemctl start [service]启动服务
systemctl stop [service]停止服务
systemctl restart [service]重启服务
systemctl status [service]查看服务当前状态
systemctl is-active [service]仅显示是否Active
systemctl list-units --type=service显示全部已经启动的服务
chmod 777 [service]	添加可执行权限

注意:在更新完[service]后,要重启服务的配置文件:systemctl daemon-reload

查看:systemctl list-unit-files

命令格式:systemctl [command] [unit]

命令[command]描述
start立刻启动后面接的 unit
stop立刻关闭后面接的 unit
restart立刻关闭后启动后面接的 unit,亦即执行 stop 再 start 的意思
reload不关闭 unit 的情况下,重新载入配置文件,让设置生效
enable设置下次开机时,后面接的 unit 会被启动
disable设置下次开机时,后面接的 unit 不会被启动
status目前后面接的这个 unit 的状态,会列出有没有正在执行、开机时是否启动等信息
is-active目前有没有正在运行中
is-enabled开机时有没有默认要启用这个 unit
kill不要被 kill 这个名字吓着了,它其实是向运行 unit 的进程发送信号
show列出 unit 的配置
mask注销 unit,注销后你就无法启动这个 unit 了
unmask取消对 unit 的注销

(3)配置防护墙

Redis需要配置 bind 0.0.0.0 不会限制IP地址

命令描述
systemctl start firewalld.service开启防火墙
systemctl stop firewalld.service关闭防火墙
systemctl disable firewalld.service禁用防火墙
systemctl mask firewalld锁定防火墙
systemctl unmask firewalld解锁防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl mask firewalld

firewall-cmd命令

参数描述
–zone作用域
–add-port=6379/tcp添加端口,格式为:端口/通讯协议
–permanent永久生效,没有此参数重启后失效

开放指定端口

firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent

重启防火墙

firewall-cmd --reload

(4)查看端口号

命令描述
netstat -ntlp查看当前所有tcp端口
netstat -ntulpgrep 6379

system 日志查看

#查看整个日志
journalctl
#查看 mytimer.timer 的日志
journalctl -u mytimer.timer
#查看 mytimer.timer 和 mytimer.service 的日志
journalctl -u mytimer
#从结尾开始查看最新日志
journalctl -f
#从结尾开始查看 mytimer.timer 的日志
journalctl -f -u timer.timer

systemctl 命令

常用命令

systemctl #范列出系统上面有启动的unit
systemctl list-units --all # 列出所有unit,包括inactive状态的
systemctl list-unit-files #列出所有已经安装的unit有哪些
systemctl list-units --all --state=inactive  # 列出所有未运行的unit,即状态是inactive的
systemctl list-units --type=service --all  #列出类型为service的所有项目,不论启动与否
systemctl get-default  #输入目前机器默认的模式,如图形界面模式或者文本模式
systemctl isolate multi-user.target  #将目前的操作环境改为纯文本模式,关掉图形界面
systemctl isolate graphical.target  #将目前的操作环境改为图形界面
systemctlpoweroff  #系统关机
systemctl reboot   #重新开机
systemctl suspend   #进入暂停模式
systemctl rescue   #强制进入救援模式
systemctl hibernate   #进入休眠模式
systemctl emergency   #强制进入紧急救援模式
systemctl list-dependencies --reverse   #查询当前默认的target关联了啥
systemctl list-dependencies graphical.target  #查询图形界面模式的target关联了啥
systemctl list-sockets   #查看当前的socket服务
systemctl show etcd.service   #查看 unit 的详细配置情况
systemctl mask etcd.service   #禁用某个服务
systemctl unmask etcd.service   #解除禁用某个服务

查看服务

#查询服务状态
systemctl status firewalld
#添加或修改配置文件后,需要重新加载
systemctl daemon-reload
#服务是否在运行
systemctl is-active firewalld
#启动、停止服务
systemctl start/stop firewalld
#是否开机自启 
systemctl is-enabled firewalld
#开机自启、禁止
systemctl enable/disable firewalld
#注销和取消注销服务
 systemctl mask/unmark cups

定时器相关命令

systemctl list-timers  # 查看所有正在运行的定时器
systemctl start/stop mytimer.timer  # 启动/停止定时器
systemctl status mytimer.timer # 查看定时器状态
systemctl enable/disable myscript.timer # 开机自启、取消自启

Timer字段说明:

OnActiveSec:定时器生效后,多少时间开始执行任务
OnBootSec:系统启动后,多少时间开始执行任务
OnStartupSec:Systemd 进程启动后,多少时间开始执行任务
OnUnitActiveSec:该单元上次执行后,等多少时间再次执行
OnUnitInactiveSec: 定时器上次关闭后多少时间,再次执行
OnCalendar:基于绝对时间,而不是相对时间执行
AccuracySec:如果因为各种原因,任务必须推迟执行,推迟的最大秒数,默认是60秒
Unit:真正要执行的任务,默认是同名的带有.service后缀的单元
Persistent:如果设置了该字段,即使定时器到时没有启动,也会自动执行相应的单元
WakeSystem:如果系统休眠,是否自动唤醒系统
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 要在Linux设置服务开机自启动,可以按照以下步骤进行操作: 1. 打开终端,使用root用户登录系统。 2. 找到需要设置开机自启动服务,可以使用以下命令查看系统中已安装的服务: systemctl list-unit-files --type=service 3. 找到需要设置开机自启动服务后,使用以下命令开启服务自启动: systemctl enable 服务名称 例如,要设置Apache服务开机自启动,可以使用以下命令: systemctl enable httpd.service 4. 确认服务已经成功设置开机自启动,可以使用以下命令查看服务状态: systemctl status 服务名称 例如,查看Apache服务状态可以使用以下命令: systemctl status httpd.service 如果服务已经成功设置开机自启动,状态应该显示为“active (running)”。 以上就是在Linux设置服务开机自启动的步骤。 ### 回答2: Linux作为一种开源操作系统,使用广泛,拥有许多强大的功能。其中一个重要的功能就是它可以方便地设置服务开机自启动服务是一种在系统启动时自动后台运行的程序,而对于许多关键服务来说,它们应该保持随时运行以确保系统正常工作。 在Linux设置服务开机自启动可以分为两部分,第一步是安装需要自启动服务程序,第二步是设置服务开机自启动。 首先,安装需要自启动服务程序。这可以通过软件包管理器来完成。以Ubuntu为例,可以使用以下命令来安装Apache服务器: sudo apt-get install apache2 完成安装后,可以使用以下命令来检查服务是否启动。 systemctl status apache2 如果服务已经启动,系统将返回一个带有“Active: active (running)”的状态消息。 接下来,让这个服务在每次系统启动时自动运行。这可以通过“systemd”服务管理器来实现。以Ubuntu为例,可以使用以下命令来启用Apache服务器自启动: sudo systemctl enable apache2 这将在系统启动时自动启动Apache服务器。您可以使用以下命令来检查服务是否已经设置自启动: sudo systemctl is-enabled apache2 如果服务已经设置自启动,系统将返回一个带有“enabled”的状态消息。 总之,Linux是一种功能强大的操作系统,可以方便地设置服务开机自启动。使用上述步骤,您可以轻松地安装并设置需要自启动服务,并确保它们在系统启动后始终运行,从而确保系统的正常工作。 ### 回答3: 在Linux中,服务是常驻进程,它可以在系统启动时自动启动,从而确保其始终运行。为了设置服务开机自启动,我们需要执行以下步骤: 第一步:确定服务名 在设置服务开机自启动之前,我们需要确保知道该服务的名称。以Nginx为例,我们可以使用以下命令来确定它的服务名称: ``` systemctl list-units | grep nginx ``` 我们可以看到Nginx服务的名称为`nginx.service`。 第二步:将服务添加到自启动列表中 为了将服务添加到开机自启动列表中,我们需要使用`systemctl enable`命令,像这样: ``` sudo systemctl enable nginx.service ``` 这个命令将在`/etc/systemd/system/`目录中创建一个符号链接以启用服务,并在系统启动时自动启动该服务。 如果我们想禁用服务开机自启动,我们可以使用`systemctl disable`命令: ``` sudo systemctl disable nginx.service ``` 第三步:重新启动系统 为了确认服务是否已成功地自动启动,我们可以重新启动系统并检查服务是否已经启动: ``` sudo reboot sudo systemctl status nginx.service ``` 执行这个命令,如果服务已成功启动并运行,我们应该看到服务状态为“active (running)”状态。 除此之外,我们还可以使用systemd-tmpfiles命令定期清理/tmp和/var/tmp目录,用于系统运行期间建立及临时调整文件使用,使用以下命令实现: ``` sudo systemctl edit systemd-tmpfiles-clean.timer ``` 打开编辑器,输入以下内容保存: ``` [Timer] OnBootSec=30s OnUnitActiveSec=1days ``` 使用该命令实现SYSTEMD-TMPFILES-CLEAN服务的刷新: ``` sudo systemctl daemon-reload ``` 最后重启服务: ``` sudo systemctl --now enable systemd-tmpfiles-clean.timer ``` 现在,我们已经成功地将服务添加到了Linux系统的开机自启动列表中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未禾

您的支持是我最宝贵的财富!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值