Getting subprocess.Popen stdout when running by cron

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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







share|improve this question



















  • Is it because /usr/sbin is in your interactive environment but not cron’s?
    – Jeff Schaller
    Jun 14 at 17:58










  • Hi @JeffSchaller, no idea.. that's why i'm asking :)
    – SagiLow
    Jun 15 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 at 9:21










  • @JeffSchaller You were close, see the answer
    – SagiLow
    Jun 15 at 9:22














up vote
0
down vote

favorite












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







share|improve this question



















  • Is it because /usr/sbin is in your interactive environment but not cron’s?
    – Jeff Schaller
    Jun 14 at 17:58










  • Hi @JeffSchaller, no idea.. that's why i'm asking :)
    – SagiLow
    Jun 15 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 at 9:21










  • @JeffSchaller You were close, see the answer
    – SagiLow
    Jun 15 at 9:22












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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







share|improve this question











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









share|improve this question










share|improve this question




share|improve this question









asked Jun 14 at 15:14









SagiLow

1537




1537











  • Is it because /usr/sbin is in your interactive environment but not cron’s?
    – Jeff Schaller
    Jun 14 at 17:58










  • Hi @JeffSchaller, no idea.. that's why i'm asking :)
    – SagiLow
    Jun 15 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 at 9:21










  • @JeffSchaller You were close, see the answer
    – SagiLow
    Jun 15 at 9:22
















  • Is it because /usr/sbin is in your interactive environment but not cron’s?
    – Jeff Schaller
    Jun 14 at 17:58










  • Hi @JeffSchaller, no idea.. that's why i'm asking :)
    – SagiLow
    Jun 15 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 at 9:21










  • @JeffSchaller You were close, see the answer
    – SagiLow
    Jun 15 at 9:22















Is it because /usr/sbin is in your interactive environment but not cron’s?
– Jeff Schaller
Jun 14 at 17:58




Is it because /usr/sbin is in your interactive environment but not cron’s?
– Jeff Schaller
Jun 14 at 17:58












Hi @JeffSchaller, no idea.. that's why i'm asking :)
– SagiLow
Jun 15 at 9:14




Hi @JeffSchaller, no idea.. that's why i'm asking :)
– SagiLow
Jun 15 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 at 9:21




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 at 9:21












@JeffSchaller You were close, see the answer
– SagiLow
Jun 15 at 9:22




@JeffSchaller You were close, see the answer
– SagiLow
Jun 15 at 9:22










1 Answer
1






active

oldest

votes

















up vote
0
down vote













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.






share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449836%2fgetting-subprocess-popen-stdout-when-running-by-cron%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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.






    share|improve this answer

























      up vote
      0
      down vote













      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.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer













        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.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jun 15 at 9:22









        SagiLow

        1537




        1537






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449836%2fgetting-subprocess-popen-stdout-when-running-by-cron%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay