这个功能非常实用啊,这样子可以通过ansible推送agent到客户端上,然后守护进程进行周期性采集数据,返回给服务端,server端再前端展示数据。
[root@k8s-master py]# cat /root/py/test.py # 更改此文件,要重启systemd restart test.service服务才行
#!/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.service
[Service]
Type = simple
User = root
Group = root
ExecStart = /usr/bin/python /root/py/test.py
StartLimitInterval = 30min
[Install]
WantedBy = multi-user.target
[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:30