Use of hdparm state in script works only manually and not 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 use the following if ... then test in an auto shutdown script which is started every 15 minutes by cron.



# Check if disk1 is currently spinning
if [ "$(hdparm -C /dev/sdb1 | grep -o "active/idle|standby")" = "active/i$
logit disk1 running, auto shutdown terminated
return 1


This works fine if I start the script manually with ./autoshutdown.sh. The disk state will be detected correctly.



But if cron is starting this script and the state is not detected the else statement is used.



Why this difference? And what do I need to change?










share|improve this question



















  • 1




    Probabhly because your $PATH differs in cron jobs, and it cannot find hdparm. SPecify the whole path to hdparm, which you can find via type -p hdparm.
    – waltinator
    Feb 10 at 16:08










  • I expanded my comment into an Answer - please Accept.
    – waltinator
    Feb 10 at 18:24














up vote
0
down vote

favorite












I use the following if ... then test in an auto shutdown script which is started every 15 minutes by cron.



# Check if disk1 is currently spinning
if [ "$(hdparm -C /dev/sdb1 | grep -o "active/idle|standby")" = "active/i$
logit disk1 running, auto shutdown terminated
return 1


This works fine if I start the script manually with ./autoshutdown.sh. The disk state will be detected correctly.



But if cron is starting this script and the state is not detected the else statement is used.



Why this difference? And what do I need to change?










share|improve this question



















  • 1




    Probabhly because your $PATH differs in cron jobs, and it cannot find hdparm. SPecify the whole path to hdparm, which you can find via type -p hdparm.
    – waltinator
    Feb 10 at 16:08










  • I expanded my comment into an Answer - please Accept.
    – waltinator
    Feb 10 at 18:24












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I use the following if ... then test in an auto shutdown script which is started every 15 minutes by cron.



# Check if disk1 is currently spinning
if [ "$(hdparm -C /dev/sdb1 | grep -o "active/idle|standby")" = "active/i$
logit disk1 running, auto shutdown terminated
return 1


This works fine if I start the script manually with ./autoshutdown.sh. The disk state will be detected correctly.



But if cron is starting this script and the state is not detected the else statement is used.



Why this difference? And what do I need to change?










share|improve this question















I use the following if ... then test in an auto shutdown script which is started every 15 minutes by cron.



# Check if disk1 is currently spinning
if [ "$(hdparm -C /dev/sdb1 | grep -o "active/idle|standby")" = "active/i$
logit disk1 running, auto shutdown terminated
return 1


This works fine if I start the script manually with ./autoshutdown.sh. The disk state will be detected correctly.



But if cron is starting this script and the state is not detected the else statement is used.



Why this difference? And what do I need to change?







bash scripts cron hdparm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 10 at 16:14









Yufenyuy Veyeh Dider

879823




879823










asked Feb 10 at 16:03









MelBourbon

155




155







  • 1




    Probabhly because your $PATH differs in cron jobs, and it cannot find hdparm. SPecify the whole path to hdparm, which you can find via type -p hdparm.
    – waltinator
    Feb 10 at 16:08










  • I expanded my comment into an Answer - please Accept.
    – waltinator
    Feb 10 at 18:24












  • 1




    Probabhly because your $PATH differs in cron jobs, and it cannot find hdparm. SPecify the whole path to hdparm, which you can find via type -p hdparm.
    – waltinator
    Feb 10 at 16:08










  • I expanded my comment into an Answer - please Accept.
    – waltinator
    Feb 10 at 18:24







1




1




Probabhly because your $PATH differs in cron jobs, and it cannot find hdparm. SPecify the whole path to hdparm, which you can find via type -p hdparm.
– waltinator
Feb 10 at 16:08




Probabhly because your $PATH differs in cron jobs, and it cannot find hdparm. SPecify the whole path to hdparm, which you can find via type -p hdparm.
– waltinator
Feb 10 at 16:08












I expanded my comment into an Answer - please Accept.
– waltinator
Feb 10 at 18:24




I expanded my comment into an Answer - please Accept.
– waltinator
Feb 10 at 18:24










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Probably because your $PATH differs in cron jobs, and it cannot find hdparm. Specify the whole path to hdparm, which you can find via type -p hdparm.



I also suggest running a cron job like:



env | sort >$HOME/cron.env
echo $PATH | tr ':' "n" >$HOME/cron.path


To see what your cron environment looks like. More generally, look at all your run environments.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    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: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2faskubuntu.com%2fquestions%2f1004887%2fuse-of-hdparm-state-in-script-works-only-manually-and-not-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
    1
    down vote



    accepted










    Probably because your $PATH differs in cron jobs, and it cannot find hdparm. Specify the whole path to hdparm, which you can find via type -p hdparm.



    I also suggest running a cron job like:



    env | sort >$HOME/cron.env
    echo $PATH | tr ':' "n" >$HOME/cron.path


    To see what your cron environment looks like. More generally, look at all your run environments.






    share|improve this answer
























      up vote
      1
      down vote



      accepted










      Probably because your $PATH differs in cron jobs, and it cannot find hdparm. Specify the whole path to hdparm, which you can find via type -p hdparm.



      I also suggest running a cron job like:



      env | sort >$HOME/cron.env
      echo $PATH | tr ':' "n" >$HOME/cron.path


      To see what your cron environment looks like. More generally, look at all your run environments.






      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Probably because your $PATH differs in cron jobs, and it cannot find hdparm. Specify the whole path to hdparm, which you can find via type -p hdparm.



        I also suggest running a cron job like:



        env | sort >$HOME/cron.env
        echo $PATH | tr ':' "n" >$HOME/cron.path


        To see what your cron environment looks like. More generally, look at all your run environments.






        share|improve this answer












        Probably because your $PATH differs in cron jobs, and it cannot find hdparm. Specify the whole path to hdparm, which you can find via type -p hdparm.



        I also suggest running a cron job like:



        env | sort >$HOME/cron.env
        echo $PATH | tr ':' "n" >$HOME/cron.path


        To see what your cron environment looks like. More generally, look at all your run environments.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 10 at 18:23









        waltinator

        20.8k74168




        20.8k74168



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1004887%2fuse-of-hdparm-state-in-script-works-only-manually-and-not-by-cron%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

            Trouble downloading packages list due to a “Hash sum mismatch” error

            How do I move numbers in filenames, in a batch renaming operation?