Subject: Re: (detached) background (console) processes
From: "Ehud Karni" <[email-masked]>
To: [email-masked]
Date: Wed, 17 Jan 2001 21:37:29 +0200

On Mon, 15 Jan 2001 17:18:28 +0200, Kaatunut <[email-masked]> wrote:
> 
> How do I create a console (well, actually, console-less) process that I
> can leave running at background- that is, no need for terminal? Under
> unix, I would just do

If you add the switch -mwindows to your gcc compilation (DO NOT change
your main to winmain !) it will make an exe that will be without console
when started from windows command line (what is really happens is that
the program has invisible window). You'll get the following warning
    /usr/lib/gcc-lib/i686-pc-cygwin/2.95.2-6/../../../../i686-pc-cygwin/bin/ld: warning: cannot find entry symbol _WinMainCRTStartup; defaulting to 00401000
that you should ignore.

At the end is of a small program that can run any program/script without
a visible window.

As for Mumit Khan <[email-masked]> remark:
>
> Note that there is more to making a program run in the background
> correctly than just using fork, and that applies to all Unix-like
> systems. You'll need to do deal session leaders and controlling ttys
> and so on (different flavors such as SysV4 and BSD do things a bit
> differently, and POSIX uses a combination of the two approaches).

Although I use `setsid ();' when needed, it is not relevant here.

Ehud.

/* run-nw.c: run any script or program with no visible window
                                                 27/12/00  */

#include <stdio.h>                         /* standard io library */
#include <stdlib.h>                        /* standard library */
#include <unistd.h>                        /* sleep , fork & exec */
#include <string.h>                        /* standard string library */
#include <errno.h>

int main ( int argc, char *argv[] )
{
char cmd [32000 ] = "" ;                   /* user command */
char *nargs [4 ] = { "/bin/bash" ,  "-c" , cmd , NULL } ;   /* execute with login /bin/bash */
int i = 1 ;

   putenv ( "BASH_ENV=/etc/bash.rc" ) ;    /* ensure bash reads my global env changes */

   while ( i < argc )                      /* do for all "real" args */
   {
       strcat ( cmd , "\"" ) ;             /* add quote before */
       strcat ( cmd , argv [ i ] ) ;       /* add the argument */
       strcat ( cmd , "\" " ) ;            /* add closing quote */
       i ++ ;
   }

   fprintf ( stderr , "Command is: |%s|\n" , cmd ) ;

   execv ( "/bin/bash" , nargs ) ;         /* exec sub command */

/* we should never reach here */
   fprintf ( stderr , "Execute failed, error = %d\n" , errno ) ;
   return ( 0 ) ;                          /* exit with no error */
}
/*==========================================================================*/

Compiled by:
gcc run-nw.c -O2 -o run-nw -static -Wall -Wno-format -Wstrict-prototypes -Wmissing-prototypes -mwindows

-- 
 @@@@@@ @@@ @@@@@@ @    @   Ehud Karni  Simon & Wiesel  Insurance agency
     @    @      @  @@  @   Tel: +972-3-6212-757    Fax: +972-3-6292-544
     @    @ @    @ @  @@    (USA)  Fax  and  voice  mail:  1-815-5509341
     @    @ @    @ @    @        Better     Safe     Than     Sorry
 http://www.simonwiesel.co.il    mailto:[email-masked]

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple