当前位置:   article > 正文

ubuntu创建守护进程,并设置开机自启动(rc.locla,rc_local.sercive)_ubuntu 开机自动启动

ubuntu 开机自动启动

1.创建守护进程

  1. vim protect.sh
  2. sudo chmod +x protect.sh
  1. #!/bin/bash
  2. # 定义所守护的进程名称或关键字
  3. TARGET_PROCESS="java"
  4. while true; do
  5. # 检测目标进程是否运行
  6. if pgrep -x "$TARGET_PROCESS" >/dev/null; then
  7. echo "The process is running."
  8. else
  9. # 启动你的项目运行脚本
  10. /path/to/run.sh &
  11. # 记录守护进程重启的日志
  12. echo "Restarting the project at $(date)" >> daemon_log.txt
  13. fi
  14. # 等待一段时间后重新检测,可以根据需要调整
  15. sleep 10
  16. done

注:为方便开机自启动,建议都写绝对路径,run.sh的也要写绝对路径。

2.rc.local添加开启启动的程序

  1. view /etc/rc.local
  2. sudo chmod +x /etc/rc.local #添加权限
  1. #!/bin/bash
  2. # rc.local
  3. # This script is executed at the end of each multiuser runlevel.
  4. # Make sure that the script will "exit 0" on success or any other
  5. # value on error.
  6. # In order to enable or disable this script just change the execution
  7. # bits.
  8. # By default this script does nothing.
  9. # 可以在 exit 0 之前添加需要开机启动的程序
  10. /path/to/protect.sh &
  11. exit 0

3.启动服务

  1. systemctl start rc-local.service
  2. 或者
  3. systemctl enable rc-local.service
  4. systemctl status rc-local.service
  5. #看active的状态
  6. #systemctl list-unit-files --type=service | grep "rc-local.service"

4..reboot kill、试试看是否成功

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/55006
推荐阅读
相关标签
  

闽ICP备14008679号