【OSX】定時実行処理

launchctl 

 

・plist作成

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>スクリプト名</string>
	<key>ProgramArguments</key>
	<array>
		<string>/bin/bash</string>
		<string>/Users/mknod/halt.sh</string>
	</array>
	<key>LowPriorityIO</key>
	<true/>
	<key>Nice</key>
	<integer>1</integer>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>20</integer>
		<key>Minute</key>
		<integer>30</integer>
	</dict>
</dict>
</plist>

 

・xxx.plistを~/Library/LaunchAgents/に設置

・launchctl load ~/Library/LaunchAgents/xxx.plist

・load確認 launchctl list

20:30に/User/mknod/halt.shが実行される。

 

サービス登録解除

・launchctl unload ~/Library/LaunchAgents/xxx.plist

 

【NET API + Python3】 add postion

Upload to NET terminal positons

Run.
> python3 this_script.py

 

# -*- coding: utf-8 -*-
import urllib.request

apk_key = 'your api key'
tid = 'terminal name'
datas = [
{"lat": 35.5xxxxxx , "lng": 139.3xxxxx, "date:": "xxxx-xx-xx xx:xx:xx" },
{"lat": 35.5xxxxxx , "lng": 139.3xxxxx, "date:": "xxxx-xx-xx xx:xx:xx" },
{"lat": 35.5xxxxxx , "lng": 139.3xxxxx, "date:": "xxxx-xx-xx xx:xx:xx" },
{"lat": 35.5xxxxxx , "lng": 139.3xxxxx, "date:": "xxxx-xx-xx xx:xx:xx" },
]

for dt in datas:

add_position = "http://api.sensor-network.jp:8181/api/position/add/"
post_data = {
'api_key': apk_key,
'tid': tid,
'lat': dt["lat"],
'lng': dt["lng"]
'date': dt["date"]
}
post_data = urllib.parse.urlencode(post_data).encode(encoding='utf-8')

request = urllib.request.Request(add_position)
request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
response = urllib.request.urlopen(request, post_data)
result = response.read()
print(result)