delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2008/03/13/03:57:20

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
Date: Thu, 13 Mar 2008 09:56:25 +0100
From: Corinna Vinschen <corinna-cygwin AT cygwin DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: How to capture error in Cygwin, $? is not working
Message-ID: <20080313085625.GC18407@calimero.vinschen.de>
Reply-To: cygwin AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
References: <16021741 DOT post AT talk DOT nabble DOT com>
MIME-Version: 1.0
In-Reply-To: <16021741.post@talk.nabble.com>
User-Agent: Mutt/1.5.16 (2007-06-09)
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

On Mar 12 23:09, nlian wrote:
> 
> Hi,
> 
> I want to write a simple script to start and stop windows service remotely.
> Here is the script:
> 
> #!/usr/bin/bash
> sc \\\\servername start "MyService"
> echo $?
> 
> =======
> The above script return the following messages: 
> [SC] StartService: OpenService FAILED 1060:
> 
> The specified service does not exist as an installed service.
> 
> 0
> =======
> 
> What I don't understand is why $? return 0 and not 1060 or any other
> error code. I want to capture the error code returned from windows program
> How to do this in Cygwin? Please advise.

What you get is the exit value from the sc command.  Sc returns 0.  What
sc prints is the error code it got from the service manager API.  It's a
pity that sc doesn't return a useful exit code but there's nothing
Cygwin can do about the exit codes of Windows tools.

`net start MyService' returns an exit code of 2, Cygwin's
`cygrunsrv -S MyService' returns 1.  Unfortunately both tools are not
capabale to start services on a remote server.

What you can do is something along the lines of

  sc \\\\servername start "MyService" | grep -q START
  echo $?
  1

Nothing keeps you from evaluating the output of the sc command
in any elaborate way you can think of.  Think grep, sed, awk, ...


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019