日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python写linux脚本_Linux下设置python脚本文件为服务

發布時間:2025/3/20 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python写linux脚本_Linux下设置python脚本文件为服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(最簡單的方式nohup python xxx.py)

-------------------------------------------------------------------------------------------------------------------

Python腳本開機自動運行;本帖適用于使用systemd的Linux系統,現在流行的Linux發行版都使用systemd。

后臺服務程序是隨系統自啟動的,我們只要把Python腳本配置為服務就行了。需要注意的一點是你Python腳本的啟動時機,它依賴不依賴其他服務(網絡連接、一些分區的掛載等等)。

#1 Python腳本

一個你要自啟動的Python腳本,我使用 /home/snail/autorun.py為例。

#2 創建Unit配置文件

1

$sudo vim/lib/systemd/system/autorun.service

寫入如下內容:

1

2

3

4

5

6

7

8

9

10

[Unit]

Description=Test Service

After=multi-user.target

[Service]

Type=idle

ExecStart=/usr/bin/python/home/snail/autorun.py

[Install]

WantedBy=multi-user.target

上面定義了一個叫 Test Service 的服務,它在multi-user環境起來之后運行;ExecStart參數指定我們要運行的程序;idle確保腳本在其他東西加載完成之后運行,它的默認值是simple。

注意使用絕對路徑。

為了獲得腳本的輸出信息,我們可以重定向到文件:

1

ExecStart=/usr/bin/python/home/snail/autorun.py>/home/snail/autorun.log2>&1

更改配置文件的權限:

1

$sudo chmod644/lib/systemd/system/autorun.service

#3 使配置文件生效

1

2

$sudo systemctl daemon-reload

$sudo systemctl enable autorun.service

#4 重啟

1

$sudo reboot

#5 查看服務狀態

1

$sudo systemctl status autorun.service

#6 服務操作命令

systemctl命令是系統服務管理器指令,它實際上將

任務

舊指令

新指令

使某服務自動啟動

chkconfig --level 3

systemctl enable httpd.service

使某服務不自動啟動

chkconfig --level 3 httpd off

systemctl disable httpd.service

檢查服務狀態

service httpd status

systemctl status httpd.service (服務詳細信息) systemctl is-active httpd.service (僅顯示是否 Active)

顯示所有已啟動的服務

chkconfig --list

systemctl list-units --type=service

啟動某服務

service httpd start

systemctl start httpd.service

停止某服務

service httpd stop

systemctl stop httpd.service

重啟某服務

service httpd restart

systemctl restart httpd.service

實例

1.啟動nfs服務

systemctl start nfs-server.service

2.設置開機自啟動

systemctl enable nfs-server.service

3.停止開機自啟動

systemctl disable nfs-server.service

4.查看服務當前狀態

systemctl status nfs-server.service

5.重新啟動某服務

systemctl restart nfs-server.service

6.查看所有已啟動的服務

systemctl list -units --type=service

開啟防火墻22端口

iptables -I INPUT -p tcp --dport 22 -j

如果仍然有問題,就可能是SELinux導致的

關閉SElinux:

修改/etc/selinux/config文件中的SELINUX=””為disabled,然后重啟。

徹底關閉防火墻:

sudo systemctl status firewalld.service

sudo systemctl stop firewalld.service

sudo systemctl disable firewalld.service

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的python写linux脚本_Linux下设置python脚本文件为服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。