亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

使用 linux 時(shí)經(jīng)常用到 ` service MySQL restart ` 等命令, 方便進(jìn)行服務(wù)的操作, 具體的服務(wù)是怎么寫(xiě)的呢,通過(guò)以下示例將了解以下內(nèi)容:

  1. 如何寫(xiě)一個(gè)簡(jiǎn)單的服務(wù)
  2. 服務(wù)異常關(guān)閉時(shí)能自動(dòng)開(kāi)啟配置

簡(jiǎn)單的示例

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ù)開(kāi)啟時(shí)執(zhí)行腳本
ExecReload=/bin/kill -HUP $MAINPID      # 服務(wù)重新加載時(shí)執(zhí)行腳本
RestartSec=5s                           # 自動(dòng)啟動(dòng)間隔時(shí)間
Restart=on-failure                      # 在什么情況下會(huì)自動(dòng)重啟

[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  # 立刻開(kāi)啟并開(kāi)機(jī)啟動(dòng)

systemctl daemon-reload #重新加載

systemctl enable nginx.service #開(kāi)機(jī)時(shí)啟動(dòng)
systemctl disable nginx.service #開(kāi)機(jī)時(shí)禁用
systemctl list-unit-files|grep enabled #已啟動(dòng)服務(wù)列表
systemctl --failed  #啟動(dòng)失敗服務(wù)列表
 

sudo update-rc.d nginx defaults #開(kāi)機(jī)時(shí)啟動(dòng)

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 [email protected]


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 啟動(dòng)一個(gè)簡(jiǎn)單的 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

分享到:
標(biāo)簽:Ubuntu service
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定