否则 ok 运行ning 代码不会在 systemd 下 运行
Otherwise ok running code won't run under systemd
我这边的另一个新手问题:)
我试图查找它,但在这种情况下我找不到任何有意义的东西。
我有这段代码,在正常情况下可以正常工作。
import automationhat
import bluetooth
import time
import sys
import telegram
bot = telegram.Bot(token='56915444444:AAEzP5jy-pMD3ZME0twxY5bXkxxxxxxxxxxxxxxxxxxd_U')
# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient
ADAFRUIT_IO_KEY = '73c755488accccccc361a506f700000002ba'
ADAFRUIT_IO_USERNAME = 'flxxxxxxxx'
def connected(client):
print('Connected to Adafruit IO! Listening for LockReg changes...')
# Subscribe to changes on a feed named LockReg.
client.subscribe('LockReg')
def disconnected(client):
print('Disconnected from Adafruit IO!')
sys.exit(1)
def message(client, feed_id, payload, retain):
print('Feed {0} received new value: {1}'.format(feed_id, payload))
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# Setup the callback functions defined above.
client.on_connect = connected
client.on_disconnect = disconnected
client.on_message = message
PHONES = ['B0:xx:2D:x0:C9:xx', 'xx:8D:08:xx:C3:7C', 'xx:AB:37:EA:93:xx']
while True:
print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
for device in PHONES:
result = bluetooth.lookup_name(device, timeout=5)
if (result != None):
# Connect to the Adafruit IO server.
client.connect()
print('Door unlocked. Nearby: %s (%s)' % (result, device))
client.publish('lock.reg', result)
automationhat.relay.one.on()
time.sleep(2)
automationhat.relay.one.off()
bot.sendMessage(-26934xxxxx, result)
time.sleep(180)
else:
print "User out of range"
print('Door locked: No Bluetooth device detected. ' )
time.sleep(10)
我的 systemd lock.service 文件如下所示:
[Unit]
Description=My Lock Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/lockreg.py > /home/pi/lock.log 2>&1
[Install]
WantedBy=multi-user.target
但是如果在初始启动时从 systemd 执行,我会收到以下错误消息:
pi@raspberrypi:~ $ systemctl status lock.service
● lock.service - My Lock Service
Loaded: loaded (/lib/systemd/system/lock.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2018-06-16 18:33:32 CEST; 29min ago
Process: 492 ExecStart=/usr/bin/python /home/pi/lockreg.py > /home/pi/lock.log 2>&1 (
Main PID: 492 (code=exited, status=1/FAILURE)
Jun 16 18:33:29 raspberrypi systemd[1]: Started My Lock Service.
Jun 16 18:33:32 raspberrypi python[492]: Traceback (most recent call last):
Jun 16 18:33:32 raspberrypi python[492]: File "/home/pi/lockreg.py", line 11, in <mod
Jun 16 18:33:32 raspberrypi python[492]: import telegram
Jun 16 18:33:32 raspberrypi python[492]: ImportError: No module named telegram
Jun 16 18:33:32 raspberrypi systemd[1]: lock.service: Main process exited, code=exited,
Jun 16 18:33:32 raspberrypi systemd[1]: lock.service: Unit entered failed state.
Jun 16 18:33:32 raspberrypi systemd[1]: lock.service: Failed with result 'exit-code'.
从 systemd 执行时,Telegram 模块无法访问。为什么?
感谢您的提示
P.
有几处可能是错误的。
可能 telegram
只为用户 pi
安装,而不是为所有用户全局安装。检查 /home/pi/.local/
看看你是否能在那里找到电报。
如果没有,您希望模块安装在系统范围的 site-packages
目录中。使用以下内容查找:
> python
Python 2.7.15 (default, May 11 2018, 15:54:10)
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> [p for p in sys.path if p.endswith('site-packages')]
['/usr/local/lib/python2.7/site-packages']
>>>
(对于您来说,这可能是一个不同的目录,因为我的 python
住在 /usr/local/bin
。)
然后检查 telegram
是否作为文件或子目录安装在 site-packages
目录中。
也可能是权限问题。 Systemd 正在 运行 将您的程序设置为某个用户 ID。检查 site-packages
目录和 telegram
子目录是否可被该特定用户读取。
编辑: Systemd
可以运行两种模式; 系统 和用户。只有后者 运行s 作为特定用户。我猜 系统 模式 运行 是 root
。从 lock.service
文件的位置,我了解到您正在 system 模式下使用 systemd。根据您的评论,telegram 仅 为用户 pi
安装。所以python
运行宁如root
应该看不到,这正是你所经历的
所以基本上你有两个选择:
- 为所有用户安装
telegram
。 (运行 安装为 root
。)
- 运行 用户模式 systemd 作为用户
pi
.
我这边的另一个新手问题:) 我试图查找它,但在这种情况下我找不到任何有意义的东西。 我有这段代码,在正常情况下可以正常工作。
import automationhat
import bluetooth
import time
import sys
import telegram
bot = telegram.Bot(token='56915444444:AAEzP5jy-pMD3ZME0twxY5bXkxxxxxxxxxxxxxxxxxxd_U')
# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient
ADAFRUIT_IO_KEY = '73c755488accccccc361a506f700000002ba'
ADAFRUIT_IO_USERNAME = 'flxxxxxxxx'
def connected(client):
print('Connected to Adafruit IO! Listening for LockReg changes...')
# Subscribe to changes on a feed named LockReg.
client.subscribe('LockReg')
def disconnected(client):
print('Disconnected from Adafruit IO!')
sys.exit(1)
def message(client, feed_id, payload, retain):
print('Feed {0} received new value: {1}'.format(feed_id, payload))
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# Setup the callback functions defined above.
client.on_connect = connected
client.on_disconnect = disconnected
client.on_message = message
PHONES = ['B0:xx:2D:x0:C9:xx', 'xx:8D:08:xx:C3:7C', 'xx:AB:37:EA:93:xx']
while True:
print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
for device in PHONES:
result = bluetooth.lookup_name(device, timeout=5)
if (result != None):
# Connect to the Adafruit IO server.
client.connect()
print('Door unlocked. Nearby: %s (%s)' % (result, device))
client.publish('lock.reg', result)
automationhat.relay.one.on()
time.sleep(2)
automationhat.relay.one.off()
bot.sendMessage(-26934xxxxx, result)
time.sleep(180)
else:
print "User out of range"
print('Door locked: No Bluetooth device detected. ' )
time.sleep(10)
我的 systemd lock.service 文件如下所示:
[Unit]
Description=My Lock Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/lockreg.py > /home/pi/lock.log 2>&1
[Install]
WantedBy=multi-user.target
但是如果在初始启动时从 systemd 执行,我会收到以下错误消息:
pi@raspberrypi:~ $ systemctl status lock.service
● lock.service - My Lock Service
Loaded: loaded (/lib/systemd/system/lock.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2018-06-16 18:33:32 CEST; 29min ago
Process: 492 ExecStart=/usr/bin/python /home/pi/lockreg.py > /home/pi/lock.log 2>&1 (
Main PID: 492 (code=exited, status=1/FAILURE)
Jun 16 18:33:29 raspberrypi systemd[1]: Started My Lock Service.
Jun 16 18:33:32 raspberrypi python[492]: Traceback (most recent call last):
Jun 16 18:33:32 raspberrypi python[492]: File "/home/pi/lockreg.py", line 11, in <mod
Jun 16 18:33:32 raspberrypi python[492]: import telegram
Jun 16 18:33:32 raspberrypi python[492]: ImportError: No module named telegram
Jun 16 18:33:32 raspberrypi systemd[1]: lock.service: Main process exited, code=exited,
Jun 16 18:33:32 raspberrypi systemd[1]: lock.service: Unit entered failed state.
Jun 16 18:33:32 raspberrypi systemd[1]: lock.service: Failed with result 'exit-code'.
从 systemd 执行时,Telegram 模块无法访问。为什么?
感谢您的提示 P.
有几处可能是错误的。
可能 telegram
只为用户 pi
安装,而不是为所有用户全局安装。检查 /home/pi/.local/
看看你是否能在那里找到电报。
如果没有,您希望模块安装在系统范围的 site-packages
目录中。使用以下内容查找:
> python
Python 2.7.15 (default, May 11 2018, 15:54:10)
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> [p for p in sys.path if p.endswith('site-packages')]
['/usr/local/lib/python2.7/site-packages']
>>>
(对于您来说,这可能是一个不同的目录,因为我的 python
住在 /usr/local/bin
。)
然后检查 telegram
是否作为文件或子目录安装在 site-packages
目录中。
也可能是权限问题。 Systemd 正在 运行 将您的程序设置为某个用户 ID。检查 site-packages
目录和 telegram
子目录是否可被该特定用户读取。
编辑: Systemd
可以运行两种模式; 系统 和用户。只有后者 运行s 作为特定用户。我猜 系统 模式 运行 是 root
。从 lock.service
文件的位置,我了解到您正在 system 模式下使用 systemd。根据您的评论,telegram 仅 为用户 pi
安装。所以python
运行宁如root
应该看不到,这正是你所经历的
所以基本上你有两个选择:
- 为所有用户安装
telegram
。 (运行 安装为root
。) - 运行 用户模式 systemd 作为用户
pi
.