前言:

以 swim 服务为例,配置linux服务自启动需要执行两步:

  • /etc/init.d 目录下添加 swim 服务的启动命令文本;
  • 使用 chkconfig 命令添加 swim 服务到多个级别下设置自启动。

详细的配置流程如下所示:

  • touch /etc/init.d/swim
  • 将启动脚本文件写入swim文件中
  • chmod 755 /etc/init.d/swim
  • chkconfig —add swim
  • chkconfig swim on

    1.编写启动脚本文件

    swim 服务为例,将其设置为 linux 服务:

  1. #!/bin/bash
  2. #chkconfig:2345 30 90
  3. #description:start or stop swim
  4. #processname:swim
  5. source /etc/profile
  6. case $1 in
  7. start) cd /home/swim;./startup.sh >> /home/swim/nohup.out;echo "swim Start-up ...";echo "The detailed information for start-up: /home/swim/nohup.out" ;;
  8. stop) /home/swim/shutdown.sh ;echo "swim shutdown success!" ;;
  9. status)
  10. if jps | grep "swim" >/dev/null; then
  11. echo "swim is running ... "
  12. else
  13. echo "swim is stopped"
  14. fi
  15. ;;
  16. restart)
  17. /home/swim/shutdown.sh > /dev/null ;echo "swim shutdown success!" ;
  18. cd /home/swim;./startup.sh >> /home/swim/nohup.out ;echo "swim Start-up ..."; echo "The detailed information for start-up: /home/swim/nohup.out" ;;
  19. *) echo "require start|stop|status|restart" ;;
  20. esac

2.将启动脚本文件设置为自启动

将 liunx 服务设置为开机自启动服务

  1. # 将swim添加到检查列表中
  2. chkconfig --add swim
  3. # 设置 swim 服务开机自启动
  4. chkconfig swim on

3.验证自启动服务设置效果

  • 验证swim服务是否配置为liunx服务

    1. # 启动服务
    2. service swim start
    3. # 关闭服务
    4. service swim stop
    5. # 重启服务
    6. service swim restart
    7. # 查看服务状态
    8. service swim status
  • 验证swim服务是否设置为自启动服务

    1. # reboot重启服务器
    2. reboot
    3. # jps查看进程是否存在
    4. jps

    4.服务配置脚本资源

    swim.sh