【发布时间】:2020-06-23 05:32:14
【问题描述】:
我有一个 python 脚本,它检查网络状态并相应地将数据写入文件。下面是代码:
cmd = "ping -c 1 www.google.com"
res = (subprocess.call(cmd, shell=True))
if res == 0:
if os.path.exists('network.txt') == False:
#File is not present, create it (This will only happen for the 1st time)
log.error("Writing data to file")
f= open('network.txt', "w+")
f.write("online")
f.close()
else:
#File is present
log.error("Writing data to file")
f= open('network.txt', "w")
f.write("online")
f.close()
else:
if os.path.exists('network.txt') == False:
#File is not present, create it (This will only happen for the 1st time)
log.error("Writing data to file")
f= open('network.txt', "w+")
f.write("offline")
f.close()
else:
#File is present
log.error("Writing data to file")
f= open('network.txt', "w")
f.write("offline")
f.close()
time.sleep(1)
当我运行此代码时,我可以看到脚本正在将数据写入online 或offline 到network.txt 文件中。所以我为此创建了一个systemd 服务,它可以继续在后台运行并且可以将数据写入文件。以下是服务:
[Unit]
Description=log health of device on regular interval
[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/Documents/health.py
Restart=on-failure
RestartSec=30s
StandardOutput=null
[Install]
WantedBy=multi-user.target
当我启动服务时,我可以看到状态为正在运行,我也可以从日志中看到Writing data to file 但没有数据写入文件。我还使用ls-lh 检查了文件的创建时间,它不会改变。这意味着服务文件中有问题,不允许它写入文件,但在我看来一切正常。
谁能帮我找出问题所在?
【问题讨论】: