linux 使用Systemd 守护进程

我们的服务器中的进程可能出现服务宕机的情况,如何在宕机的时候自动重启,比如我们的mongo数据,这里我说下Systemd的方案

  1. /etc/systemd/system/目录下创建mongodb.service
    简单步骤 1.创建.service文件,2.sudo systemctl enable xxx 3.sudo systemctl start xxxx 即可

2.编写内容

[Unit]

Description=mongodb

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

ExecStart=/etc/init.d/mongodb start

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/etc/init.d/mongodb stop

Restart=always

RestartSec=60

PrivateTmp=true

[Install]

WantedBy=multi-user.target

3.保存并关闭文件。

4.重新加载 Systemd 管理器配置:

5.sudo systemctl daemon-reload
启动 MongoDB 服务:

6.sudo systemctl enable mongodb
检查 MongoDB 服务状态:

7.sudo systemctl start mongodb
设置 MongoDB 服务开机自启:

8.sudo systemctl status mongodb

9.若想移除
sudo systemctl disable mongodb

若出现启动失败情况,先停止,在启动
sudo systemctl stop mongodb
sudo systemctl start mongodb

验证,使用kill命令杀死线程,服务会在指定时间自己启动

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容