手機(jī)觸屏網(wǎng)站模板學(xué)seo優(yōu)化
目錄
- 創(chuàng)建服務(wù)
- 創(chuàng)建另一個(gè)服務(wù),用于執(zhí)行更新操作
- 給你的用戶配置一些systemctl命令權(quán)限
創(chuàng)建服務(wù)
/etc/systemd/system下新建服務(wù)配置文件:yourapp.service,內(nèi)容如下:
[Unit]
Description=yourapp
After=network.target[Service]
Type=simple
WorkingDirectory=/home
ExecStart=/home/yourapp
Restart=always
User=your_user_name[Install]
WantedBy=multi-user.target
如果你是修改現(xiàn)有的配置文件,需要讓systemctl重載一下:
systemctl stop yourapp
systemctl disable yourapp.service
systemctl daemon-reload
啟動(dòng)服務(wù):
systemctl enable yourapp
systemctl start yourapp
創(chuàng)建另一個(gè)服務(wù),用于執(zhí)行更新操作
程序要更新,不能自己下載更新文件,然后覆蓋自己,有可能會(huì)報(bào)異常:文件無法改寫。
也不能啟動(dòng)另一個(gè)進(jìn)程,讓那個(gè)進(jìn)程來停止本服務(wù),并更新文件,因?yàn)榉?wù)停止時(shí),該服務(wù)下的所有子進(jìn)程,也會(huì)被kill掉,
所以,要啟動(dòng)另一個(gè)服務(wù)來執(zhí)行更新操作。
假設(shè)執(zhí)行這個(gè)操作的程序在 upgrade/app,/etc/systemd/system下新建服務(wù)配置文件:yourapp_update.service,內(nèi)容如下:
[Unit]
Description=yourapp_update
After=network.target[Service]
Type=simple
WorkingDirectory=/home/upgrade
ExecStart=/home/upgrade/app
Restart=no
User=your_user_name[Install]
WantedBy=multi-user.target
注意:Restart=no,這個(gè)程序無須自動(dòng)重啟
激活服務(wù),無須啟動(dòng)該服務(wù):
systemctl enable yourapp_update
這個(gè)更新程序,先調(diào)用 systemctl stop yourapp 停止服務(wù), 再把新的程序文件覆蓋到現(xiàn)有文件,然后調(diào)用 systemctl restart yourapp 重啟主程序的服務(wù),但是,your_user_name通常沒有執(zhí)行這個(gè)命令的權(quán)限,往下看,教你如何給your_user_name分配systemctl的權(quán)限。
給你的用戶配置一些systemctl命令權(quán)限
通常你的服務(wù)不是root啟動(dòng)的,所以程序里,也就無法調(diào)用systemctl命令,下面要給運(yùn)行服務(wù)的用戶,授予一些systemctl權(quán)限。
在/etc/sudoers.d路徑下,創(chuàng)建文件 yourapp,確認(rèn)此文件所有人為 root,文件權(quán)限是 440(chmod 440 文件名)。
文件內(nèi)容:
your_user_name ALL=(ALL) NOPASSWD: /bin/systemctl restart yourapp.service
your_user_name ALL=(ALL) NOPASSWD: /bin/systemctl start yourapp.service
your_user_name ALL=(ALL) NOPASSWD: /bin/systemctl stop yourapp.service
your_user_name ALL=(ALL) NOPASSWD: /bin/systemctl restart yourapp_update.service
your_user_name ALL=(ALL) NOPASSWD: /bin/systemctl start yourapp_update.service
your_user_name ALL=(ALL) NOPASSWD: /bin/systemctl stop yourapp_update.service
這樣,your_user_name 用戶就有控制這兩個(gè)服務(wù)的權(quán)限了。