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


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':
                            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:

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
  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 )

I figured it out !!!:

The reason behind why it worked is still obscure but it has empiricly worked out so im happy.

The fix was to change the check_output command from the original

result = check_output(['service','hostapd','status'])

to the now working

result = check_output(["/usr/sbin/service hostapd status"], shell=True )

first the shell=True part was necessary then a quick search with which service gave me the location for the service "service". After that phython was pleased.

The weirdest thing about this whole thing is that phython had no problem with an expression that i used in a latter part of the same phython script.


EDIT: Now that i researched this to write that answer i realised that the call fuction had a small "sudo" that my check_output didnt have.

EDIT: So of course i went back and modified the check_output function to also have a simple sudo and it freaking worked without the hassle of the shell=True and what so ever.

FINAL SOLUTION that worked for me :


result = check_output(['service','hostapd','status'])


result = check_output(['sudo','service','hostapd','status'])
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.