mcv*_*mcv 2 scripts services systemd
[Unit]
Description=captive portal automation
[Service]
Type=simple
ExecStart=/usr/bin/python /home/pi/do.py
Restart=on-failure
RestartSec=5
Run Code Online (Sandbox Code Playgroud)
以上是caportal.service
在Ubuntu 14.04机器上运行的一个名为systemd服务的内容。我需要每 30 分钟重复一次该服务。我怎样才能做到这一点 ?
/home/pi/do.py
import requests,json
import netifaces as ni
import commands as cm
gateway=ni.gateways()['default'][ni.AF_INET][0]
IPaddr=ni.ifaddresses('wlan0')[ni.AF_INET][0]['addr']
mac=ni.ifaddresses('wlan0')[ni.AF_LINK][0]['addr'].upper().replace(':','-')
ssid=cm.getoutput('iwgetid -r')
Home="http://"+gateway+":8010/"
URL=Home+"login.html"
print URL
d={}
d['IdSession']=mac
d['Language']='English'
d['refrescar']='0'
d['ip']=IPaddr
d['mac']=mac
d['DSP']=Home
d['AC']='1'
d['userlog']='vishnu'
d['userpass']='12345'
d['read']='checkbox'
d['Login']='+++Go+++'
try:
if ssid=='machCochin':
r=requests.post(URL,data=d)
print r.status_code
#raise ValueError("ERROR simulated")
else:
print "network is not machCochin"
except Exception as e:
pass
Run Code Online (Sandbox Code Playgroud)
systemd提供了 cron 作业的模拟。它们被称为计时器。
您需要创建一个.service
文件来运行脚本,并创建一个.timer
具有匹配文件名的文件来安排它。
就像是:
[Unit]
Description=run my script
[Timer]
OnCalendar=*-*-* *:00/5:05
Persistent=true
[Install]
WantedBy=timers.target
Run Code Online (Sandbox Code Playgroud)
例如每 5 分钟运行一次。我也让您挖掘手册页或搜索引擎来查找OnCalendar
指令或其他内容。
您甚至可以通过将两个文件放入~/.config/systemd/user/
或以 root 身份运行/etc/systemd/system
您需要激活计时器才能在重新启动后运行
sudo systemctl enable myservice.timer
Run Code Online (Sandbox Code Playgroud)
如果您以用户身份运行,请添加--user
到上面的命令(并且不使用 sudo)./config
然后,运行它
sudo systemctl start myservice.timer
Run Code Online (Sandbox Code Playgroud)
或者
systemctl --user start myservice.timer
Run Code Online (Sandbox Code Playgroud)