# TWAPI example # Restart the specified Windows service # Usage example: tclsh restartservice.example SERVICENAME package require twapi if {$argc != 1} { puts stderr "Usage: [info nameofexecutable] $argv0 SERVICENAME" exit 1 } # Convert the service name to the internal name set svcname [twapi::get_service_internal_name [lindex $argv 0]] # Abort if other running services are dependent on it set dependents [twapi::get_dependent_service_status $svcname -active] if {[llength $dependents]} { puts stderr "Service not restarted as the following services are dependent on it:" foreach dependent $dependents { array set svcinfo $dependent puts "$svcinfo(display_name)" } exit 1 } # Stop the service - wait up to 10 seconds if {![twapi::stop_service $svcname -wait 10000]} { puts stderr "Service could not be stopped" exit 1 } # Start the service - wait up to 30 seconds if {![twapi::start_service $svcname -wait 30000]} { puts stderr "Service could not be started" exit 1 }