1

i created a script which runs on boot which checks if there's an internet connection on my raspberry pi, and at the same time updates the time (care of ntp) - via os.system().

import datetime, os, socket, subprocess
from time import sleep

dir_path = os.path.dirname(os.path.abspath(__file__))

def internet(host="8.8.8.8"):
    result = subprocess.call("ping -c 1 "+host, stdout=open(os.devnull,'w'), shell=True)
    if result == 0:
        return True
    else:
        return False


timestr = time.strftime("%Y-%m-%d--%H:%M:%S")
netstatus = internet()

while netstatus == False:
    sleep(30)
    netstatus = internet()

if netstatus == True:
    print "successfully connected! updating time . . . "
    os.system("sudo bash "+dir_path+"/updatetime.sh") 
    print "time updated! time check %s"%datetime.datetime.now()

where updatetime.sh contains the following:

service ntp stop
ntpd -q -g
service ntp start

this script runs at reboot/boot and i'm running this in our workplace, 24/7. also, outputs from scripts like these are saved in a log file. it's working fine, but is there a way how NOT to output connect: Network is unreachable if there's no internet connection? thanks.

edit

i run this script via a shell script i named launch.sh which runs check_net.py (this script's name), and other preliminary scripts, and i placed launch.sh in my crontab to run on boot/reboot:

@reboot sh /home/pi/launch.sh > /home/pi/logs/cronlog 2>&1

from what i've read in this thread: what does '>/dev/null/ 2>&1' mean, 2 handles stderr where as 1 handles stdout.

i am new to this. I wish to see my stdout - but not the stderrs (in this case, the connect: Network is unreachable messages (only)..

/ogs

Ogs
  • 167
  • 3
  • 15
  • 1
    Redirect the errors to /dev/null? For example, call the script like this: "updatetime.sh 2> /dev/null" – Matt Runion Sep 22 '18 at 00:32
  • have you tried writing stderr to /dev/null? – gregory Sep 22 '18 at 00:32
  • hello @mrunion i'm only concerned about omitting the 'connect: Network is unreachable' part w/c i believe is generated by my subprocess.call(...). correct me if i'm wrong, but i thought writing the stdout attribute to os.devnull is similar to writing to /dev/null? thank you. /ogs – Ogs Sep 22 '18 at 00:36
  • Well, it is. But you are writing "stdout" to /dev/null". There is also an errorout channel -- that's what the "2" in the "2> /dev/null" is handling. I have not ran your code, but it appears that any errors encountered from your subprocess call will not be on stdout, but on the errorout -- thus they are not written to /dev/null. – Matt Runion Sep 22 '18 at 00:40
  • i see. i will dabble further. thank you for your inputs sirs. :) /ogs – Ogs Sep 22 '18 at 00:41
  • hello sir @mrunion i added some context above. your addt'l inputs are appreciated. /ogs – Ogs Sep 22 '18 at 01:16
  • 1
    It's not really a good idea to delete stderr msgs. As you know about "connect:..", then that is OK, but I'd recommend reading this thread, https://stackoverflow.com/a/2342841/620097 as you can save you stdOut to one file and then use `grep -v connect:...` to eliminate the error messages you don't want, but redirect the rest of it with something like `2>/tmp/StdErr.$$.log` so you don't loose any important error msgs. Good luck. – shellter Sep 22 '18 at 17:47
  • thanks for your inputs @shellter i'm trying out something, and will confirm later here if it works. /ogs. – Ogs Sep 22 '18 at 18:30

1 Answers1

0

As per @shellter 's link suggestion in the comments, i restructured my cron to:

@reboot sh /home/pi/launch.sh 2>&1 > /home/pi/logs/cronlog | grep "connect: Network is unreachable" 

alternatively, i also came up of an alternative solution, which involves a different way of checking an internet connection with the use urllib2.urlopen():

def internet_init():
    try:
        urllib2.urlopen('https://www.google.com', timeout=1)
        return True
    except urllib2.URLError as err:
        return False

either of the two methods above omitted any connect: Network is unreachable error output in my logs.

thanks!

/ogs

Ogs
  • 167
  • 3
  • 15
  • 1
    I'm glad you're making progress, but I don't see how `2>&1 > cronlog| grep "connect"` is removing the `connect` messages you don't want to see. Are you sure the error occured during your testing and that the msg was generated? If so, then look at the local email for the user that owns the `cron` job you have set up. I would expect to see the `connect` errors in there. To delete messages from a stream, you want `grep -v srchTarget`, but once you have redirected stderr and stdout into a file, there won't really be anything getting sent to the pipe and the grep after it. (I could be wrong ;-) ). – shellter Sep 23 '18 at 16:09
  • yeah i'm sure. i also tested it in my second raspberry pi unit where i emulated an internet outage, and the "connect: Network is unreachable" error doesn't appear in /home/pi/logs/cronlog -- this file btw is emailed via ssmtp at the end of the day. do note that the restructured cron applies to the original internet() function stated above, and not the internet_init() function. thanks. – Ogs Sep 24 '18 at 02:57