473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

exit code on windows

Is there a straightforward way to get the exit code from a windows
process?

I want to write a script that runs a bunch of .sql files against SQL
Server using osql. I want it to stop if there's an error; e.g.,

import os
import glob

server = 'foo'
database = 'bar'
template = 'osql -s%(server)s -d%(database)s -f%(name)s'
for name in glob.glob('*.sql'):
cmd = template % locals()
stdin, stdout = os.popen2(cmd)
output = stdout.read()
print stdin.close()
print stdout.close()
# On Unix I'd use the return value from the fd.close()
# to get the exit code, but on Windows the return values
# here are:
# None
# None

Does that mean the way I'd get the exit code on Unix has no hope of
working on Windows?

Thanks,

// m
Jul 18 '05 #1
1 4652
Mark McEahern <ma*******@mceahern.com> writes:
Is there a straightforward way to get the exit code from a windows
process?
It's the result of the close on the handle returned from the os.popen*
calls. For Windows, in the case of the variants returning more than a
single handle, you'll get the exit code when you close the last
handle, and unlike Unix, it's not a "wait"-encoded value but just the
actual result code from the child process.
I want to write a script that runs a bunch of .sql files against SQL
Server using osql. I want it to stop if there's an error; e.g.,

import os
import glob

server = 'foo'
database = 'bar'
template = 'osql -s%(server)s -d%(database)s -f%(name)s'
for name in glob.glob('*.sql'):
cmd = template % locals()
stdin, stdout = os.popen2(cmd)
output = stdout.read()
print stdin.close()
print stdout.close()
# On Unix I'd use the return value from the fd.close()
# to get the exit code, but on Windows the return values
# here are:
# None
# None
The above should work fine, although actually I'm interested in your
Unix comment since I thought under Unix you had to use popen2.Popen3/4
and then use wait() on the resulting objects to get an exit code. But
in any event, it should be fine under Windows (with the final child
process result coming back from the stdout.close() in the above
example).

Are you sure that osql is returning a non-zero result? Zero result
codes are translated to a None return.

A quick test for me seems to work:

rc.py

import sys

sys.exit(10)

rctest.py

import os

stdin, stdout = os.popen2('python rc.py')
output = stdout.read()
print stdin.close()
print stdout.close()
python rctest.py

None
10

Now this is with Win2K (or NT/XP). The Win9x series do have a bit of
an issue bubbling up result codes in general, but Python has a helper
module (w9xpopen.exe) that should work around that. One except in
either case can be if you explictly use a top level
command.com/cmd.exe since I think they end up absorbing the exit code
of the command they run.

-- David

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guinness Mann | last post by:
Pardon me if this is not the optimum newsgroup for this post, but it's the only .NET newsgroup I read and I'm certain someone here can help me. I have a C# program that checks for an error...
11
by: Peter Steele | last post by:
I have a Windows application written in C# that I want to return a non-zero exit code when it it run. The problem is that as a Windows application, there doesn't seem to be a way to control this....
1
by: Ioannis Vranos | last post by:
I am currently reading a chapter involving multithreading, and some sample code calls Environment::Exit() to terminate the application with all threads. What is the difference from...
22
by: Shelby | last post by:
Hi, I used System.Windows.Forms.Application.DoEvents() in a loop to handle user click close button . Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)...
8
by: Zeno Lee | last post by:
What is the best way to return an exit code from a VB.NET windows forms app? My Forms application is dual purpose. It is an interactive windows app. It is also automated and run via a script and...
1
by: =?Utf-8?B?VGFvZ2U=?= | last post by:
Hi All, When I use applcation.exit() in winForm application, the form closed, but the process is still going!! ( The debug process is still running if debug in VS IDE). Environment.Exit(0) works...
2
by: Learning.Net | last post by:
hi , I have a application which reads files, directory,and its version and version information is written to text file.Its working fine if files in directory are less but problem arises when no...
4
by: Quill_Patricia | last post by:
I have a Python script which is used to load data into a database. Up to now this script has been run by customers from the Windows command prompt using "python edg_loader.pyc". Any error messages...
9
by: titanandrews | last post by:
Hi All, Is there any way to catch the exit code from someone calling exit(status) and check the value before the program terminates? Just for an idea, the code I'm thinking of is something...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.