Ubuntu Service腳本編寫示例
作者:陳月生的草稿箱
使用 Linux 時(shí)經(jīng)常用到 ` service mysql restart ` 等命令, 方便進(jìn)行服務(wù)的操作, 具體的服務(wù)是怎么寫的呢,通過以下示例將了解以下內(nèi)容。
使用 Linux 時(shí)經(jīng)常用到 ` service mysql restart ` 等命令, 方便進(jìn)行服務(wù)的操作, 具體的服務(wù)是怎么寫的呢,通過以下示例將了解以下內(nèi)容:
- 如何寫一個(gè)簡單的服務(wù)
- 服務(wù)異常關(guān)閉時(shí)能自動開啟配置
簡單的示例
nano /lib/systemd/system/xx.service
- [Unit]
- Description=Check GPU INFO by chenwei # 服務(wù)描述
- Wants=network-online.target # 服務(wù)依賴于網(wǎng)絡(luò)
- After=network-online.target
- [Service]
- Type=simple
- ExecStart=/root/shell/agent/chkgpu # 服務(wù)開啟時(shí)執(zhí)行腳本
- ExecReload=/bin/kill -HUP $MAINPID # 服務(wù)重新加載時(shí)執(zhí)行腳本
- RestartSec=5s # 自動啟動間隔時(shí)間
- Restart=on-failure # 在什么情況下會自動重啟
- [Install]
- WantedBy=multi-user.target
- [Unit]
- Description=Advanced key-value store
- After=network.target
- [Service]
- Type=forking
- ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
- ExecStop=/bin/kill -s TERM $MAINPID
- PIDFile=/var/run/redis_6379.pid
- Restart=always
- RestartSec=5s
- Restart=on-failure
- [Install]
- WantedBy=multi-user.target
- Alias=redis.service
nginx 示例
- [Unit]
- Description=A high performance web server and a reverse proxy server
- After=network.target
- [Service]
- Type=forking
- PIDFile=/var/run/nginx.pid
- #ExecStartPre=/usr/local/nginx/sbin/nginx
- ExecStart=/usr/sbin/nginx
- ExecReload=/usr/sbin/nginx -s reload
- ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
- TimeoutStopSec=5
- KillMode=mixed
- [Install]
- WantedBy=multi-user.target
常用命令
- systemctl enable --now nginx.service # 立刻開啟并開機(jī)啟動
- systemctl daemon-reload #重新加載
- systemctl enable nginx.service #開機(jī)時(shí)啟動
- systemctl disable nginx.service #開機(jī)時(shí)禁用
- systemctl list-unit-files|grep enabled #已啟動服務(wù)列表
- systemctl --failed #啟動失敗服務(wù)列表
wssh
- file=/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
issh
- file=/usr/bin/issh
- mv $file $file.bak
- cat <<EOF >>$file
- #!/bin/bash
- wssh
- autossh -M 10111 -NR 0.0.0.0:11111:localhost:22 pc@1.10sh.cn
- EOF
- cat $file
- chmod +x $file
- file=/lib/systemd/system/issh.service
- mv $file $file.bak
- cat <<EOF >>$file
- [Unit]
- Description=autossh shell to connect to my server by chenwei. #sudo apt install autossh
- Wants=network-online.target
- After=network-online.target
- [Service]
- Type=simple
- ExecStart=/usr/bin/issh
- ExecReload=/bin/kill -HUP
- RestartSec=5s
- Restart=on-failure
- [Install]
- WantedBy=multi-user.target
- EOF
- cat $file
- systemctl enable --now issh.service
- systemctl status issh.service
pweb
使用python 啟動一個(gè)簡單的 http 文件服務(wù)。
- sudo -i
- file=/home/pweb.sh
- mv $file $file.bak
- cat <<EOF >>$file
- #!/bin/bash
- python3 -m http.server
- EOF
- cat $file
- chmod +x $file
- file=/lib/systemd/system/pweb.service
- mv $file $file.bak
- cat <<EOF >>$file
- [Unit]
- Description=Simple python pweb by chenwei.
- Wants=network-online.target
- After=network-online.target
- [Service]
- Type=simple
- ExecStart=/home/pweb.sh
- ExecReload=/bin/kill -HUP
- RestartSec=5s
- Restart=on-failure
- [Install]
- WantedBy=multi-user.target
- EOF
- cat $file
- systemctl enable --now pweb.service
- systemctl status pweb.service
責(zé)任編輯:未麗燕
來源:
今日頭條