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

设置为 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

参数详解

  • 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

测试

我们来测试一下 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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值