I am a total beginner and wanted to play around with Python's check_output function.
I have a user based cron job that opens a bash file which in turn opens a Python script to be executed on boot up. I am running my Raspberry pi headless but am using screen to be able to check up on the Python process if I wanted to SSH in.
My user crontab looks like this and starts perfectly fine as the Python script actually starts and almost works.
@reboot sleep 30 && sh /home/pi/launcher.sh >>/home/pi/logs/cronlog 2>&1
This is the bash file that starts screen and then the Python script
#!/bin/sh screen -h 500 -S Telegram -d -m /usr/bin/python /home/pi/telegram/bot2.py
This is the Python file that has the issues. I can start it just fine and can verify that it is working. But check_output gives me an error.
import os import sys import time import telepot from telepot.loop import MessageLoop from subprocess import call from subprocess import check_output from subprocess import CalledProcessError print 'Hello' def handle(msg): chat_id = msg['chat']['id'] command = msg['text'] print 'Got command: %s' % command if command == 'Status': try: result = check_output(['service','hostapd','status']) except CalledProcessError as exc: result = exc.output MessageLoop(bot, handle).run_as_thread() print 'I am listening ...' while 1: time.sleep(10)
This is the error I get. Interestingly, simple call processes do not give me that error and are working fine.
Got command: Status Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/telepot/loop.py", line 37, in run_forever self._handle(msg) File "/home/pi/telegram/bot2.py", line 31, in handle result = check_output(['service','hostapd','status']) File "/usr/lib/python2.7/subprocess.py", line 212, in check_output process = Popen(stdout=PIPE, *popenargs, **kwargs) File "/usr/lib/python2.7/subprocess.py", line 390, in __init__ errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1024, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory
I hope someone can help me. I have a feeling that it has something to do with permissions or environments on boot.
It is very important to add that the whole thing works like a charm when I go in manually and start the bash file myself when I'm logged in.
EDIT: I did some poking around and I get a different error now:
Got command: Status /bin/sh: 1: service: not found
I changed the command in the Python script to
result = check_output(["service hostapd status"], shell=True )