0

I have a small Orange Pi One Server running Armbian (Ubuntu based) and it has a script that checks web server running on Rasberry Pi2 running LibreElec 8.0 is available and if not it power cycles the server (Rasberry Pi) controlled via a 433 Mhz remote controlled socket via a web server on an ESP8266 unit. All the units are on my home network; no external access.

The script works fine when I run it from the command line – I see the logic flow, etc., and I get the desired results. I have a line in my crontab to run the check on the hour. However I don't see any output in the log file that I've specified in the redirect on the line in my crontab. I know the crontab entry is running as an empty logfile is created.

This is the line in my crontab. I'm trying to output the date and time and the messages to a file so I know if and when tests were run and restarts initiated.

I realise the output from wget is sent to dev\null but I expect my echo commands to be logged. These outline the logic flow in the script which is what I'd like to be logged.

59 0-23 * * */root/loftRPTCheck.sh | while IFS= read -r line; do echo "$(date) $line"; done >> /root/RPTloftlog.txt

Here is the command I used to run the check manually and the output on my PuTTY terminal:

root@orangepione:~# ./loftRPTCheck.sh
--2017-05-18 21:05:05--  http://192.168.0.143:9091/
Connecting to 192.168.0.143:9091... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
Retrying.

--2017-05-18 21:05:13--  (try: 2)  http://192.168.0.143:9091/
Connecting to 192.168.0.143:9091... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
Retrying.

--2017-05-18 21:05:22--  (try: 3)  http://192.168.0.143:9091/
Connecting to 192.168.0.143:9091... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
Retrying.

--2017-05-18 21:05:32--  (try: 4)  http://192.168.0.143:9091/
Connecting to 192.168.0.143:9091... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
Giving up.

wget to Loft RP Transmission failed
Power off Loft RP
--2017-05-18 21:05:39--  http://192.168.0.237/LOFTOFF
Connecting to 192.168.0.237:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘/dev/null’

/dev/null                           [ <=>                                                  ]      68  --.-KB/s    in 0s

2017-05-18 21:05:41 (1.23 MB/s) - ‘/dev/null’ saved [68]

Sleep 10s
Power on Loft RP
--2017-05-18 21:05:51--  http://192.168.0.237/LOFTON
Connecting to 192.168.0.237:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘/dev/null’

/dev/null                           [ <=>                                                  ]      68  --.-KB/s    in 0s

2017-05-18 21:05:54 (1.26 MB/s) - ‘/dev/null’ saved [68]

Sleep 20s
Retrying Loft Tranismission..
--2017-05-18 21:06:14--  http://192.168.0.143:9091/
Connecting to 192.168.0.143:9091... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: /transmission/web/ [following]
--2017-05-18 21:06:14--  http://192.168.0.143:9091/transmission/web/
Reusing existing connection to 192.168.0.143:9091.
HTTP request sent, awaiting response... 200 OK
Length: 24139 (24K) [text/html]
Saving to: ‘/dev/null’

/dev/null                       100%[=====================================================>]  23.57K  --.-KB/s    in 0s

2017-05-18 21:06:14 (50.7 MB/s) - ‘/dev/null’ saved [24139/24139]

Restart worked ok.

So that looks OK as the messages are output when run from the command line, but I'm not sure how best to log this output to a file with the date and time at the start of each line.

This is the shell script that is run:

root@orangepione:~# cat loftRPTCheck.sh
wget --tries=4 --timeout=7 -O/dev/null http://192.168.0.143:9091

if [[ $? -ne 0 ]]; then
    echo "wget to Loft RP Transmission failed"
    echo "Power off Loft RP"
    wget -O/dev/null http://192.168.0.237/LOFTOFF
    echo "Sleep 10s"
    sleep 10
    echo "Power on Loft RP"
    wget -O/dev/null http://192.168.0.237/LOFTON
    echo "Sleep 20s"
    sleep 20
    echo "Retrying Loft Transmission.."
    wget  --tries=4 --timeout=7 -O/dev/null http://192.168.0.143:9091
    if [[ $? -ne 0 ]]; then
        echo "RP not restarted???"
    else
        echo "Restart worked ok."
    fi
    exit 1;
fi
  • 1
    Try putting a space between the * and the /root/loftRPTCheck.sh in your crontab. – Scott May 18 '17 at 20:57
  • 1
    I would avoid piping in crontab, create a single script instead. – Archemar May 19 '17 at 6:29
  • @Archemar: I (also) cringe whenever I see a command in crontab that’s more than a shell simple command (i.e., a program, optionally with arguments, optionally with < and/or > I/O redirections), but I’m not sure why.  Can you explain a reason for this aversion? – Scott May 20 '17 at 16:18
  • when cron see ls | wc will it behave the way you want (aka. bash), or just call ls with | (pipe) as first arguement and wc as second ? – Archemar May 20 '17 at 16:25
0

logger command will help you. http://manpages.ubuntu.com/manpages/precise/en/man1/logger.1.html

  • Thanks for this will start to use this as its syslogd based. Not sure why the output is not being sent to the file I think it might be the wget pipe to dev null. I've hard coded my commands to pipe to a log file I'll update them to use logger. Thank you. – newbie May 19 '17 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.