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.  
  6. [Service] 
  7. Type=simple 
  8. ExecStart=/root/shell/agent/chkgpu      # 服务开启时执行脚本 
  9. ExecReload=/bin/kill -HUP $MAINPID      # 服务重新加载时执行脚本 
  10. RestartSec=5s                           # 自动启动间隔时间 
  11. Restart=on-failure                      # 在什么情况下会自动重启 
  12.  
  13. [Install] 
  14. WantedBy=multi-user.target   
  15.  
  16. [Unit] 
  17. Description=Advanced key-value store 
  18. After=network.target 
  19.  
  20. [Service] 
  21. Type=forking 
  22. ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf 
  23. ExecStop=/bin/kill -s TERM $MAINPID 
  24. PIDFile=/var/run/redis_6379.pid 
  25. Restart=always 
  26. RestartSec=5s 
  27. Restart=on-failure 
  28.  
  29.  
  30. [Install] 
  31. WantedBy=multi-user.target 
  32. Alias=redis.service 

nginx 示例 

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

常用命令 

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

wssh 

  1. file=/lib/systemd/system/myssh.service mv $file $file.bak cat «EOF »$file 
  2.  
  3. [Unit] Description=Web SSH server by chenwei. pip install webssh Wants=network-online.target 
  4. After=network-online.target 
  5.  
  6. [Service] Type=simple ExecStart=wssh ExecReload=/bin/kill -HUP $MAINPID 
  7. RestartSec=5s 
  8. Restart=on-failure 
  9.  
  10. [Install] WantedBy=multi-user.target 
  11.  
  12. 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.  
  8.  
  9. EOF 
  10. cat $file 
  11.  
  12. chmod +x  $file 
  13.  
  14. file=/lib/systemd/system/issh.service 
  15. mv $file $file.bak 
  16.  
  17. cat <<EOF >>$file 
  18.   
  19. [Unit] 
  20. Description=autossh shell to connect to my server by chenwei.  #sudo apt  install autossh 
  21.  
  22. Wants=network-online.target 
  23. After=network-online.target 
  24.  
  25. [Service] 
  26. Type=simple 
  27. ExecStart=/usr/bin/issh 
  28. ExecReload=/bin/kill -HUP 
  29. RestartSec=5s 
  30. Restart=on-failure 
  31.  
  32. [Install] 
  33. WantedBy=multi-user.target 
  34.  
  35.  
  36. EOF 
  37. cat $file 
  38.  
  39.  
  40. systemctl enable --now issh.service 
  41.  
  42. systemctl status issh.service 

pweb

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

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

鸿蒙官方战略合作共建——HarmonyOS技术社区

相关内容