def main(options, args):
    """
    Create a subprocess to start the Envmon subsystem.  Then wait for user
    to terminate it with ^C
    """

    # Is user trying to kill an existing instance?
    if options.kill:
        try:
            proc = myproc.getproc(pidfile=options.pidfile)

        except myproc.myprocError:
            print "Failed to read PID file: %s" % options.pidfile
            sys.exit(1)

        # Check status of process.
        if proc.status().startswith('exited'):
            print "Envmon processes already exited"
            sys.exit(1)

        # Attempt to kill process group
        proc.signalpg(signal.SIGKILL)
        sys.exit(0)

    if options.logfile:
        if options.logstderr:
            print "Sorry, cannot use --stderr and --log together in this app!"
            sys.exit(1)

        try:
            f_out = open(options.logfile, 'a')

        except IOError, e:
            print "Cannot open stdout/stderr log file: %s" % str(e)
            sys.exit(1)
def status(options):
    if not options.pidfile:
        print "Please specify a --pidfile"
        sys.exit(1)
        
    print "Looking for LTCS process..."
    try:
        child = myproc.getproc(pidfile=options.pidfile)
        print child.status()

    except myproc.myprocError, e:
        print "Error getting PID for LTCS process; please check manually"
def stop(options):
    if not options.pidfile:
        print "Please specify a --pidfile"
        sys.exit(1)
        
    print "Looking for LTCS process..."
    try:
        child = myproc.getproc(pidfile=options.pidfile)
        if child.status != 'exited':
            print "Trying to stop LTCS process..."
            child.kill()
            sys.exit(0)
        else:
            print "No LTCS process found."
            sys.exit(1)
    except myproc.myprocError, e:
        print "Error getting PID for LTCS process; please check manually"