2018-4-27 python實現systemd進程後臺運行任務

 

   這個功能很是實用啊,這樣子能夠經過ansible推送agent到客戶端上,而後守護進程進行週期性採集數據,返回給服務端,server端再前端展現數據。前端

 

[root@k8s-master py]# cat /root/py/test.py  # 更改此文件,要重啓systemd restart test.service服務才行python

 

#!/usr/bin/env python # -*- coding=utf-8 -*-
import time filepath = '/root/py/test_systemd' # 文件路徑
fm = '%Y-%m-%d %X'
def get_time(): while 1: nowtime = time.strftime(fm, time.localtime()) with open(filepath, 'a') as fp:  # a模式就表明寫形式的追加
 fp.write(nowtime) fp.write('\n') time.sleep(5) if __name__ == '__main__': get_time()

 

[root@k8s-master py]# touch /root/py/test_systemd
[root@k8s-master py]# cat /etc/systemd/system/test.service
[Unit]
Description = test daemon
After = rc.local.servicespa

[Service]
Type = simple
User = root
Group = root
ExecStart = /usr/bin/python /root/py/test.py
StartLimitInterval = 30minrest

[Install]
WantedBy = multi-user.targetcode


[root@k8s-master py]# systemctl start test.service && systemctl enable test.service
[root@k8s-master py]# ps -ef |grep python
[root@k8s-master py]# tail -f /root/py/test_systemd
2018-04-23 11:41:15
2018-04-23 11:41:20
2018-04-23 11:41:25
2018-04-23 11:41:30server

相關文章
相關標籤/搜索