LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with setting cron job with if condition (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-setting-cron-job-with-if-condition-4175560430/)

dragon777 12-02-2015 12:24 PM

Help with setting cron job with if condition
 
Hi guys,
I own a vu+ solo se v2 box and the external usb hdd spins down automatically every 5 minutes.
To fix this I used successfully this cron job:
*/4 * * * * touch /media/hdd/keepalive.log
The problem is that I would like this cron job to NOT work when the vu+ BOX is in standby so the hdd can have a rest.
For this purpose I had some help but something is missing I guess because it does not work.
Here is what I tried:

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

-crontab file to:
Code:
*/4 * * * * /root/powerstate.sh 2>&1 > /dev/null

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

says:
<e2powerstate>
<e2instandby>false</e2instandby>
</e2powerstate>

Any ideas, please??? and remember that I have no clue about all this, so be patient

berndbausch 12-02-2015 05:12 PM

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.

chrism01 12-02-2015 05:14 PM

Something like
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


All times are GMT -5. The time now is 10:18 AM.