Mongodb 设置自动重启服务 (CentOS Systemctl)_mongodb自动重启

CSDN博客 · · 494 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

设置为 Systemctl 进程服务启动

  • 可以方便启动/停止
  • 可以让进程挂掉时重启

创建服务配置

/usr/lib/systemd/system/mongodb.service

[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target  
  
[Service]  
Type=forking  
ExecStart=/usr/local/bin/mongod --config /home/www/mongo/conf/mongo.server.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/bin/mongod --shutdown --config /home/www/mongo/conf/mongo.server.conf
PrivateTmp=true  
Restart=always
RestartSec=1 

[Install]  
WantedBy=multi-user.target
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

参数详解

  • Restart = always
    只要不是通过systemctl stop来停止服务,任何情况下都必须重启服务,默认值为no
  • RestartSec = 5
    重启间隔,比如某次异常后,等待5(s)再进行启动,默认值0.1(s)
  • StartLimitInterval
    无限次重启,默认是10秒内如果重启超过5次则不再重启,设置为0表示不限次数重启

启动

// 启动服务
$ systemctl start mongodb.service
// 查看是否启动成功
$ systemctl status mongodb.service

// 设置开机启动
$ systemctl enable mongodb.service
// systemctl disable mongodb.service

// 如果是 start 服务后修改 配置,需要重新加载服务的配置文件
// $ systemctl daemon-reload
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

测试

我们来测试一下 kill 进程,看看服务是否会重启

// 查看端口
$ ps -ef | grep mongod
// root   26072   1  2 10:01 .. mongod --config ../mongo.conf
$ kill -9 26072
// 等待几秒后再次查看端口
$ ps -ef | grep mongod
// root   1412   1  2 10:01 .. mongod --config ../mongo.conf
// 你会发现进程号不一样了,因为进程已经杀掉重启了
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

本文来自:CSDN博客

感谢作者:CSDN博客

查看原文:Mongodb 设置自动重启服务 (CentOS Systemctl)_mongodb自动重启

494 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传