Ubuntu service 脚本编写 示例

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

  1. 如何写一个简单的服务
  2. 服务异常关闭时能自动开启配置
简单的示例nano /lib/systemd/system/xx.service
[Unit]Description=Check GPU INFO by chenwei# 服务描述Wants=network-online.target# 服务依赖于网络After=network-online.target[Service]Type=simpleExecStart=/root/shell/agent/chkgpu# 服务开启时执行脚本ExecReload=/bin/kill -HUP $MAINPID# 服务重新加载时执行脚本RestartSec=5s# 自动启动间隔时间Restart=on-failure# 在什么情况下会自动重启[Install]WantedBy=multi-user.target[Unit]Description=Advanced key-value storeAfter=network.target[Service]Type=forkingExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.confExecStop=/bin/kill -s TERM $MAINPIDPIDFile=/var/run/redis_6379.pidRestart=alwaysRestartSec=5sRestart=on-failure[Install]WantedBy=multi-user.targetAlias=redis.serviceNginx 示例[Unit]Description=A high performance web server and a reverse proxy serverAfter=network.target[Service]Type=forkingPIDFile=/var/run/nginx.pid#ExecStartPre=/usr/local/nginx/sbin/nginx ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reloadExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pidTimeoutStopSec=5KillMode=mixed[Install]WantedBy=multi-user.target常用命令systemctl enable --now nginx.service# 立刻开启并开机启动systemctl daemon-reload #重新加载systemctl enable nginx.service #开机时启动systemctl disable nginx.service #开机时禁用systemctl list-unit-files|grep enabled #已启动服务列表systemctl --failed#启动失败服务列表 sudo update-rc.d nginx defaults #开机时启动
wsshfile=/lib/systemd/system/myssh.service mv $file $file.bak cat «EOF »$file
[Unit] Description=Web SSH server by chenwei. pip install webssh Wants=network-online.target
After=network-online.target
[Service] Type=simple ExecStart=wssh ExecReload=/bin/kill -HUP $MAINPID
RestartSec=5s
Restart=on-failure
[Install] WantedBy=multi-user.target
EOF cat $file
isshfile=/usr/bin/isshmv $file $file.bakcat <<EOF >>$file#!/bin/bashwsshautossh -M 10111 -NR 0.0.0.0:11111:localhost:22 pc@1.10sh.cnEOFcat $filechmod +x$filefile=/lib/systemd/system/issh.servicemv $file $file.bakcat <<EOF >>$file [Unit]Description=autossh shell to connect to my server by chenwei.#sudo aptinstall autosshWants=network-online.targetAfter=network-online.target[Service]Type=simpleExecStart=/usr/bin/isshExecReload=/bin/kill -HUPRestartSec=5sRestart=on-failure[Install]WantedBy=multi-user.targetEOFcat $filesystemctl enable --now issh.servicesystemctl status issh.servicepweb使用Python 启动一个简单的 http 文件服务
sudo -ifile=/home/pweb.shmv $file $file.bakcat <<EOF >>$file#!/bin/bashpython3 -m http.serverEOFcat $filechmod +x$filefile=/lib/systemd/system/pweb.servicemv $file $file.bakcat <<EOF >>$file [Unit]Description=Simple python pweb by chenwei.Wants=network-online.targetAfter=network-online.target[Service]Type=simpleExecStart=/home/pweb.shExecReload=/bin/kill -HUPRestartSec=5sRestart=on-failure[Install]WantedBy=multi-user.targetEOFcat $filesystemctl enable --now pweb.servicesystemctl status pweb.service
【Ubuntu service 脚本编写 示例】


    推荐阅读