• Linux使用systemctl实现开机自启动


    1、在部署目录下新建 startup.sh 脚本(注意java路径要改为绝对路径)。然后检查该脚本是否可以执行。

    1. #!/bin/sh
    2. WORKDIR=/home/
    3. TARGET=test.jar
    4. start(){
    5. pid=`get_pid`
    6. now=`date "+%Y-%m-%d %H:%M:%S"`
    7. if [ -z "$pid" ]; then
    8. new_target
    9. pid=`get_pid`
    10. printf '%s start %s done, with pid %s.\n' "$now" "$TARGET" "$pid"
    11. else
    12. printf '%s %s is running, with pid %s.\n' "$now" "$TARGET" "$pid"
    13. fi
    14. }
    15. new_target(){
    16. if [ -f "$WORKDIR"/"$TARGET" ]; then
    17. exec /usr/local/jdk1.8.0_381/bin/java -Xms2048m -Xmx4096m -jar "$WORKDIR"/"$TARGET" >/dev/null 2>&1 &
    18. else
    19. now=`date "+%Y-%m-%d %H:%M:%S"`
    20. printf '%s %s file not exist.\n' "$now" "$WORKDIR"/"$TARGET"
    21. fi
    22. }
    23. stop(){
    24. pid=`get_pid`
    25. now=`date "+%Y-%m-%d %H:%M:%S"`
    26. if [ -z "$pid" ]; then
    27. printf '%s %s is NOT running, ignored.\n' "$now" "$TARGET"
    28. else
    29. printf '%s try to kill %s, with pid %s.\n' "$now" "$TARGET" "$pid"
    30. kill "$pid"
    31. wait_till_killed
    32. fi
    33. }
    34. wait_till_killed(){
    35. wait_time=60
    36. while [ $wait_time -ge 0 ]
    37. do
    38. sleep 1
    39. pid=`get_pid`
    40. now=`date "+%Y-%m-%d %H:%M:%S"`
    41. if [ -z "$pid" ]; then
    42. printf '%s stop %s done.\n' "$now" "$TARGET"
    43. return
    44. else
    45. printf '%s wait till killed, with pid %s.\n' "$now" "$pid"
    46. fi
    47. let wait_time--
    48. done
    49. kill -9 "$pid"
    50. printf '%s FORCE stop %s done.\n' "$now" "$TARGET"
    51. }
    52. status(){
    53. pid=`get_pid`
    54. now=`date "+%Y-%m-%d %H:%M:%S"`
    55. if [ -z "$pid" ]; then
    56. printf '%s %s is NOT running.\n' "$now" "$TARGET"
    57. else
    58. printf '%s %s is running, with pid %s.\n' "$now" "$TARGET" "$pid"
    59. fi
    60. }
    61. get_pid() {
    62. pid=`ps -aux | grep "$TARGET" | grep -v grep | awk '{print $2}'`
    63. echo "$pid"
    64. }
    65. check(){
    66. pid=`get_pid`
    67. now=`date "+%Y-%m-%d %H:%M:%S"`
    68. if [ -z "$pid" ]; then
    69. printf '%s %s is NOT running, call by watch dog.\n' "$now" "$TARGET"
    70. start
    71. fi
    72. }
    73. monitor(){
    74. now=`date "+%Y-%m-%d %H:%M:%S"`
    75. printf '%s %s start to monitor.\n' "$now" "$TARGET"
    76. while [ 1 ]
    77. do
    78. sleep 10
    79. check
    80. done
    81. now=`date "+%Y-%m-%d %H:%M:%S"`
    82. printf '%s %s stop to monitor.\n' "$now" "$TARGET"
    83. }
    84. echo "---------------------------------------------------"
    85. echo " Start/Stop/Status script"
    86. echo " Work dir: $WORKDIR "
    87. echo " APP: $TARGET "
    88. echo "---------------------------------------------------"
    89. case "$1" in
    90. start)
    91. start
    92. ;;
    93. stop)
    94. stop
    95. ;;
    96. status)
    97. status
    98. ;;
    99. monitor)
    100. monitor
    101. ;;
    102. check)
    103. check
    104. ;;
    105. restart)
    106. stop
    107. start
    108. ;;
    109. *)
    110. printf 'Usage: %s {start|stop|status|restart|monitor|check}\n' "$0"
    111. exit 1
    112. ;;
    113. esac

    2、在 /etc/systemctl/system 目录下,新建test.service 文件。内容如下

    1. [Unit]
    2. After=network-online.target
    3. Wants=network-online.target
    4. [Service]
    5. WorkingDirectory=/home/
    6. Type=forking
    7. ExecStart=/bin/bash ./startup.sh start
    8. ExecStop=/bin/bash ./startup.sh stop
    9. TimeoutSec=65
    10. RestartSec=10
    11. Restart=always
    12. StartLimitInterval=0
    13. LimitNOFILE=65535
    14. LimitNPROC=65535
    15. LimitCORE=65535
    16. KillMode=process
    17. SuccessExitStatus=143
    18. [Install]
    19. WantedBy=multi-user.target

    3、修改了service文件,需要执行以下命令更新一下。

    systemctl daemon-reload

    4、设置开机自启动

    systemctl enable test.service

    5、执行以下命令,启动/关闭/查看状态。

    1. systemctl start test.service
    2. systemctl stop test.service
    3. systemctl status test.service

  • 相关阅读:
    QMDBC63-200、QMDBD80-300、QMDBT100-200气缸
    队列的实现(c语言)
    接口的安全设计要素:ticket,签名,时间戳
    制作Windows下可移植Python环境和使用cx_Oracle
    C++ - 包装器
    Navicat Premium 16 安装及卸载
    【2024最新精简版】Redis面试篇
    “开放SDK,创造无限可能”,「四维轻云」又有新功能上线!攻略来了
    嵌入式软件开发之程序架构设计-任务调度
    Linux--忘记root密码解决办法
  • 原文地址:https://blog.csdn.net/qq_45932382/article/details/133807652