Ubuntu Service脚本编写示例

使用 Linux 时经常用到 ` service mysql restart ` 等命令, 方便进行服务的操作, 具体的服务是怎么写的呢,通过以下示例将了解以下内容:

  • 如何写一个简单的服务
  • 服务异常关闭时能自动开启配置

简单的示例

nano /lib/systemd/system/xx.service

  1. [Unit]
  2. Description=Check GPU INFO by chenwei # 服务描述
  3. Wants=network-online.target # 服务依赖于网络
  4. After=network-online.target
  5. [Service]
  6. Type=simple
  7. ExecStart=/root/shell/agent/chkgpu # 服务开启时执行脚本
  8. ExecReload=/bin/kill -HUP $MAINPID # 服务重新加载时执行脚本
  9. RestartSec=5s # 自动启动间隔时间
  10. Restart=on-failure # 在什么情况下会自动重启
  11. [Install]
  12. WantedBy=multi-user.target
  13. [Unit]
  14. Description=Advanced key-value store
  15. After=network.target
  16. [Service]
  17. Type=forking
  18. ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
  19. ExecStop=/bin/kill -s TERM $MAINPID
  20. PIDFile=/var/run/redis_6379.pid
  21. Restart=always
  22. RestartSec=5s
  23. Restart=on-failure
  24. [Install]
  25. WantedBy=multi-user.target
  26. Alias=redis.service

nginx 示例

  1. [Unit]
  2. Description=A high performance web server and a reverse proxy server
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. PIDFile=/var/run/nginx.pid
  7. #ExecStartPre=/usr/local/nginx/sbin/nginx
  8. ExecStart=/usr/sbin/nginx
  9. ExecReload=/usr/sbin/nginx -s reload
  10. ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
  11. TimeoutStopSec=5
  12. KillMode=mixed
  13. [Install]
  14. WantedBy=multi-user.target

常用命令

  1. systemctl enable --now nginx.service # 立刻开启并开机启动
  2. systemctl daemon-reload #重新加载
  3. systemctl enable nginx.service #开机时启动
  4. systemctl disable nginx.service #开机时禁用
  5. systemctl list-unit-files|grep enabled #已启动服务列表
  6. systemctl --failed #启动失败服务列表

wssh

  1. file=/lib/systemd/system/myssh.service mv $file $file.bak cat «EOF »$file
  2. [Unit] Description=Web SSH server by chenwei. pip install webssh Wants=network-online.target
  3. After=network-online.target
  4. [Service] Type=simple ExecStart=wssh ExecReload=/bin/kill -HUP $MAINPID
  5. RestartSec=5s
  6. Restart=on-failure
  7. [Install] WantedBy=multi-user.target
  8. EOF cat $file

issh

  1. file=/usr/bin/issh
  2. mv $file $file.bak
  3. cat <<EOF >>$file
  4. #!/bin/bash
  5. wssh
  6. autossh -M 10111 -NR 0.0.0.0:11111:localhost:22 pc@1.10sh.cn
  7. EOF
  8. cat $file
  9. chmod +x $file
  10. file=/lib/systemd/system/issh.service
  11. mv $file $file.bak
  12. cat <<EOF >>$file
  13. [Unit]
  14. Description=autossh shell to connect to my server by chenwei. #sudo apt install autossh
  15. Wants=network-online.target
  16. After=network-online.target
  17. [Service]
  18. Type=simple
  19. ExecStart=/usr/bin/issh
  20. ExecReload=/bin/kill -HUP
  21. RestartSec=5s
  22. Restart=on-failure
  23. [Install]
  24. WantedBy=multi-user.target
  25. EOF
  26. cat $file
  27. systemctl enable --now issh.service
  28. systemctl status issh.service

pweb

使用python 启动一个简单的 http 文件服务。

  1. sudo -i
  2. file=/home/pweb.sh
  3. mv $file $file.bak
  4. cat <<EOF >>$file
  5. #!/bin/bash
  6. python3 -m http.server
  7. EOF
  8. cat $file
  9. chmod +x $file
  10. file=/lib/systemd/system/pweb.service
  11. mv $file $file.bak
  12. cat <<EOF >>$file
  13. [Unit]
  14. Description=Simple python pweb by chenwei.
  15. Wants=network-online.target
  16. After=network-online.target
  17. [Service]
  18. Type=simple
  19. ExecStart=/home/pweb.sh
  20. ExecReload=/bin/kill -HUP
  21. RestartSec=5s
  22. Restart=on-failure
  23. [Install]
  24. WantedBy=multi-user.target
  25. EOF
  26. cat $file
  27. systemctl enable --now pweb.service
  28. systemctl status pweb.service

原文地址:https://www.toutiao.com/a7005179018735518246/

本文链接:https://my.lmcjl.com/post/6071.html

展开阅读全文

4 评论

留下您的评论.