home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / internal / 1812 < prev    next >
Encoding:
Internet Message Format  |  1992-09-15  |  2.3 KB

  1. Path: sparky!uunet!dove!cme!libes
  2. From: libes@cme.nist.gov (Don Libes)
  3. Newsgroups: comp.unix.internals
  4. Subject: Re: How can a Unix process put itself in background?
  5. Message-ID: <19292@muffin.cme.nist.gov>
  6. Date: 15 Sep 92 18:37:44 GMT
  7. References: <lma.716056953@dayton.Stanford.EDU> <lma.716092591@dayton.Stanford.EDU>
  8. Organization: National Institute of Standards and Technology
  9. Lines: 37
  10.  
  11. In article <lma.716092591@dayton.Stanford.EDU> lma@dayton.Stanford.EDU (Larry Augustin) writes:
  12. }lma@dayton.Stanford.EDU (Larry Augustin) writes:
  13. }>A user invokes a program at the shell, and interacts with the program
  14. }>for a short time.  Eventually the user selects some "quit" option of
  15. }>the program.  Before the program really quits, it wants to go off and
  16. }>spend a minute or two checking some database files on disk.  Is there
  17. }>any way for the program to return control to the user's shell while it
  18. }>does the background stuff?  In effect, the program wants to "detach"
  19. }>itself from stdin/stdout, return control to the shell, and continue in
  20. }>background for a while before exiting.
  21. }
  22. }>The only solutions we could come up with were based on forking a child
  23. }>to do the background part.  is this the only way to do it?  Is there a
  24. }>solution that does not need a fork?
  25.  
  26. }The consensus seems to be that it can't be done (without forking), as
  27. }whatever shell exec'd the program will be stuck in a wait(), and could
  28. }only continue upon death of the child (or some interaction by the
  29. }user).  There would have to be some other convention for the running
  30. }program to tell the shell to continue.
  31.  
  32. Here's a bit of Expect to do this.  It works as follows - when you are
  33. ready to detach the program, you type "~detach".  Expect puts itself
  34. into the background, tells the program to do the "longrunningcommand",
  35. and when done, tells it to exit gracefully.  Obviously Expect is doing
  36. the fork for you.  Nonetheless this is the easiest way to solve the
  37. whole problem without hacking up the original program. - Don
  38.  
  39. spawn prog                      ;# start program
  40. interact ~detach {
  41.         if [fork] exit          ;# parent exits
  42.         disconnect
  43.         send "longrunningcommand\r"
  44.         expect $prompt          ;# wait for prog to say it is ready to quit
  45.         send "quit\r"           ;# tell program to quit
  46.         expect eof
  47. }
  48.