这个功能很是实用啊,这样子能够经过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