I want to get a service status and if it's not up, to send the status (stdout) in email.
This script is scheduled to run every hour by cron.
When running manually, the following works fine:

def is_service_running(name):
with open(os.devnull, 'wb') as hide_output:
    proc = subprocess.Popen(['service', name, 'status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        output = proc.stdout.read()
        exit_code = proc.wait()
        return exit_code == 0, output

But when running by cron. output is empty.
How can I capture stdout when running by cron? Thank you

  • Is it because /usr/sbin is in your interactive environment but not cron’s?
    – Jeff Schaller
    Jun 14, 2018 at 17:58
  • Hi @JeffSchaller, no idea.. that's why i'm asking :)
    – SagiLow
    Jun 15, 2018 at 9:14
  • I think you should see if that popen call is successful. Maybe also use the full path to the service command.
    – Jeff Schaller
    Jun 15, 2018 at 9:21
  • @JeffSchaller You were close, see the answer
    – SagiLow
    Jun 15, 2018 at 9:22

1 Answer 1


The problem wasn't cron but shell=True.
Apparently, when using shell=True, popen expects single string and not a list.
So when I updated my call to:

proc = subprocess.Popen(['service ' + name + ' status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

everything worked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .