Cron Job (conditioned)

There are 11 replies in this Thread which was already clicked 802 times. The last Post () by joeuser.

  • Hi!
    I am looking for help setting a cron job so it does not work when i switch my box to standby.
    It looks like this now:
    */4 * * * * touch /media/hdd/keepalive.log
    and it does what it is intended for but I would like that it does not do it all the time and that the hdd gets some rest while the box is on standby.
    I read that I need to use if or while to condition the job to work only when the box is active/on, but I have no clue how to do it.
    any help, please...

  • Not sure this is the best way, but you can try something like this script:


    powerstate.sh

    Bash
    #!/bin/sh
    if (wget -O - -q http://localhost/web/powerstate|grep -i true) then
        touch /media/hdd/keepalive.log
    fi


    Then change the crontab file to:

    Code
    */4 * * * * /root/powerstate.sh 2>&1 > /dev/null


    You may have to modify a little depending on your system. From command prompt, try just:

    Code
    wget -O -  http://localhost/web/powerstate


    To see what the output is....

  • hi joeuser,
    Thanks for your reply.
    I really have no clue how to do any of this. The line I was using to keep the external hdd from going to sleep I found somewhere and just put it in crontab, but I really have no clue.
    I own a vu+ solo se v2 box and I got very excited with your answer and I think I did what you said, but it is not working.
    I used ftp to put the script powerstate.sh in user/script and changed root and bh files in etc/bhcron.
    This last part I do not know how to do:
    You may have to modify a little depending on your system. From command prompt, try just:
    Code:
    wget -O - http://localhost/web/powerstate
    To see what the output is....
    Anyway, I am pretty sure that you are code is good but I just do not know where it should be in the box for it to work...
    Thanks again for your time and I how you can help me figure it out.

  • You can try from a webrowser:

    Code
    http://<ip of box>/web/powerstate


    Did you change permissions on powerstate.sh to 755 (read,write,execute)?
    What is in the cron now? YOu have to make cron point to the right directory where you put powerstate.sh (if not in /root)...

  • Hi again,
    It is still not working but i think it is very close to be working.
    I will tell you how things are right now after I did what you said.
    -powerstate.sh looks like you suggested from the beginning, it is located in root and permissions changed to 755
    -then root and bh files in etc/bhcron also look like you suggested
    -http://<ip of box>/web/powerstate in webrowser shows:
    <e2powerstate>
    <e2instandby>false</e2instandby>
    </e2powerstate>


    What am I missing?
    In powerstate.sh file, do I need to write ip of box instead of word localhost?


    Best regards and thanks again for your time and help

  • I am trying that now. But I wonder, wouldnt it mean that before it would keep the hdd alive when in standby? and it wasnt doing it


    - - - Updated - - -


    not working... just tried it

  • can you telnet to the box and run:

    Code
    bash -x /root/powerstate.sh


    Output should

    Code
    bash -x powerstate.sh
    + wget -O - -q http://localhost/web/powerstate
    + grep -i false
    false   </e2instandby>
    + touch /media/hdd/keepalive.log

    look like

    Edited once, last by joeuser ().

  • Telnet says it cannot open it.
    I go to it via ftp and right click on permissions and it is set to 755 so I do not know what is going on.


    - - - Updated - - -


    In powerstate.sh file, do I need to write ip of box instead of word localhost?


    - - - Updated - - -


    I see that this line (false </e2instandby>) is not in powerstate.sh. does it have to be?

  • What exactly does it say when you telnet to the box and run the command?


    No, localhost should be find. If you want to test, you can telnet and run just the wget command.

    Code
    wget -O - http://localhost/web/powerstate


    No, we just need to look for the word "false" somewhere in the response.

  • Everything is working. The problem was, as you pointed out, incorrect path in crontab to .sh file. I put the .sh in the right location and all is fine now.
    Just in case it can be good for you, I got these recommendations from another site:


    (The parentheses around the if condition are unlikely to work. Remove them.
    Inside cron, you have a very limited environment, so that you need to use absolute pathnames for commands like wget.)


    And:
    Code:
    #!/bin/bash
    wget -O - -q http://localhost/web/powerstate|grep -i false >/dev/null 2>&1
    if [[ $? -eq 0 ]] # check results code of last cmd in pipeline
    then
    touch /media/hdd/keepalive.log
    fi
    ?
    (NB: I don't have the HW to test this....


    Also, as above, use full paths for cmds in cron jobs)




    I will keep it as you suggested because it is working fine, but I wonder if there is any merit in these comments...
    __________________

  • He does not know what you are using the script on (hint: " I don't have the HW to test this..." ) so he wants to make a completely generic/compatible script. Since he could not test, he tried to point out all the possible reasons a script "may" not work on "some" machines.


    Key is that it works on your box.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!