Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Correct way to check if command executed successfully

51 views
Skip to first unread message

Cecil Westerhof

unread,
Mar 17, 2018, 5:28:06 AM3/17/18
to
After being disconnected from the internet for six hours I decided to
write something to check my internet connection. The core would be:
if {[catch {exec ping -q -c 1 -W 1 ${pingDomain} >&/dev/null}]} {
puts "NOT connected"
} else {
puts Connected
}

Is this the best way to check if a command executed successfully? That
is the only thing I am interested in: was it executed successfully or
not.

Or maybe I should change the first line to:
set cmd [list exec ping -q -c 1 -W 1 ${pingDomain} >&/dev/null]
if {[catch ${cmd}]} {

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

stefan

unread,
Mar 17, 2018, 7:29:36 AM3/17/18
to
> Is this the best way to check if a command executed successfully?

set fh [open |[list ping -c 1 example.org]]
if {[catch {close $fh} e opts]} {
lassign [dict get $opts -errorcode] msg pid exitcode
puts $msg
puts $pid
puts $exitcode
}

open/close will give you access to the child process' details. You can then watch out for CHILDSTATUS, CHILDKILLED & friends.

On a different page: When the aim is to test internet connectivity, why not use Tcl's [socket]. Btw., what you are after has been done before, check the wiki: http://wiki.tcl.tk/41924

HTH,
Stefan

Cecil Westerhof

unread,
Mar 18, 2018, 6:59:07 AM3/18/18
to
stefan <stefan....@wu.ac.at> writes:

>> Is this the best way to check if a command executed successfully?
>
> set fh [open |[list ping -c 1 example.org]]
> if {[catch {close $fh} e opts]} {
> lassign [dict get $opts -errorcode] msg pid exitcode
> puts $msg
> puts $pid
> puts $exitcode
> }
>
> open/close will give you access to the child process' details. You can
> then watch out for CHILDSTATUS, CHILDKILLED & friends.

Looks interesting. If I do it in the shell it seems to work. But if I
do it in a program I get:
CHILDKILLED
12516
SIGPIPE


> On a different page: When the aim is to test internet connectivity, why
> not use Tcl's [socket]. Btw., what you are after has been done before,
> check the wiki: http://wiki.tcl.tk/41924

I will look into those.

Cecil Westerhof

unread,
Mar 18, 2018, 8:59:05 AM3/18/18
to
stefan <stefan....@wu.ac.at> writes:

>> Is this the best way to check if a command executed successfully?
>
> set fh [open |[list ping -c 1 example.org]]
> if {[catch {close $fh} e opts]} {
> lassign [dict get $opts -errorcode] msg pid exitcode
> puts $msg
> puts $pid
> puts $exitcode
> }
>
> open/close will give you access to the child process' details. You can
> then watch out for CHILDSTATUS, CHILDKILLED & friends.

That does not seem to work. (See my other reply.) But I used to better
some things.

I now initialise the following:
set cmd [list exec ping -c 1 -W 1 ${pingDomain} >/dev/null]
set intervalCheck 1
set notConnected "ping: ${pingDomain}: Temporary failure in name resolution"

And have the following proc defined:
proc checkInternetConnection {} {
if {[catch ${::cmd} errorStr opts]} {
if {${errorStr} == ${::notConnected}} {
set message "NOT connected"
} else {
set message "OTHER ERROR: ${errorStr}"
}
} else {
set message Connected
}
showMessage ${message} True
after [getWaitMinutesTicks ${::intervalCheck}] checkInternetConnection
}

showMessage is to show a message. For example:
13:35:00: Connected
13:36:40: NOT connected
13:37:40: NOT connected
13:38:00: Connected

And I use:
after [getWaitMinutesTicks ${::intervalCheck}] checkInternetConnection

To make sure that the check is run at the start of every minute.
0 new messages