服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Ubuntu - Ubuntu Service脚本编写示例

Ubuntu Service脚本编写示例

2021-09-28 23:04陈月生的草稿箱 Ubuntu

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

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 

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

延伸 · 阅读

精彩推荐
  • UbuntuUbuntu17.10怎么添加日历事项? Ubuntu添加行程提醒的教程

    Ubuntu17.10怎么添加日历事项? Ubuntu添加行程提醒的教程

    Ubuntu17.10怎么添加日历事项?Ubuntu17.10系统中有一个日程管理功能,可以在日历中添加行程提醒,下面我们就来看看Ubuntu添加行程提醒的教程,需要的朋友可...

    服务器之家2812019-06-19
  • UbuntuUbuntu20.04开启root账户的方法步骤

    Ubuntu20.04开启root账户的方法步骤

    这篇文章主要介绍了Ubuntu20.04开启root账户的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    缀梦13002020-08-01
  • UbuntuUbuntu安装和卸载CUDA和CUDNN的实现

    Ubuntu安装和卸载CUDA和CUDNN的实现

    这篇文章主要介绍了Ubuntu安装和卸载CUDA和CUDNN的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    夜雨飘零14632020-08-04
  • UbuntuWSL、WSL2与Ubuntu性能大PK

    WSL、WSL2与Ubuntu性能大PK

    科技媒体 Phoronix 对 Windows 10 May 2020 中 WSL 和 WSL 2 的性能进行了测试,参与测试的发行版为 Ubuntu 20.04 on WSL/WSL2 ,以及 Ubuntu 20.04 LTS,均被安装在除 Windows 之...

    开源中国29412020-06-23
  • UbuntuUbuntu Gnome下如何修改应用图标icon?

    Ubuntu Gnome下如何修改应用图标icon?

    最近有些朋友问小编Ubuntu Gnome下如何修改应用图标icon?今天小编要为大家分享的是Ubuntu Gnome下修改应用图标icon的方法;有需要的朋友一起去看看吧...

    服务器之家6412019-06-01
  • Ubuntu如何在ubuntu系统中安装pycharm工具并运行

    如何在ubuntu系统中安装pycharm工具并运行

    在Windows系统中安装pycharm,只需要下载安装包,然后根据指令一步一步操作;而在Linux系统中的Ubuntu中安装pycharm,需要下载安装包,还有安装相关的其他软件...

    百度经验12082019-10-21
  • UbuntuUbuntu下安装Chrome的方法分享

    Ubuntu下安装Chrome的方法分享

    本文给大家分享的是Ubuntu下安装Chrome的方法,安装的过程中发现还是挺麻烦的,就记录下来推荐给大家,有需要的小伙伴可以参考下。...

    Ubuntu教程网3862021-10-25
  • UbuntuUbuntu 15.04升级到Ubuntu 15.10的详细教程

    Ubuntu 15.04升级到Ubuntu 15.10的详细教程

    ubuntu15.04怎么升级到ubuntu15.10?又该升级系统了,但是很多人对ubuntu系统很不熟悉,下面我们一起来看看ubuntu15.04升级ubuntu15.10的详细教程,需要的朋友可以...

    服务器之家4562019-07-02