SUMMARY: [was: nohup ...] Detaching/attaching jobs from terminal

From: Dimitris Hatzopoulos (dimitris@aliakmon.cperi.forth.gr)
Date: Fri Jun 12 1992 - 05:10:52 CDT


Hello!

First of all, I want to express my gratitude to all those who responded
to my question, about making an already running process immune to
hangups etc. As I mentioned in my mail, I was seeking for a way to use
Unix's Csh like I used IBM VM/CMS mainframes.

Here, I'm posting a summary of all answers I've received so far, to
the following Newsgroups: comp.unix.questions,comp.unix.shell,
comp.sys.sun.admin.

What seems to be the most appropriate solution to my problem, was the
suggestion of Sebastian [svb%svb.guug.de@freya.svb.guug.de], which I
have put at the top. I have included the other related mails for the
benefit of anyone else.

Best regards,

D.

---------------------------------------------------------------------

>From svb%svb.guug.de@freya.svb.guug.de Fri Jun 12 11:15:43 1992

[Sebastian has kindly tried to use Greek, in his first lines :-)]

Ok, I give in, ascii-greek kills me: What I wanted to say is, use the
PD program "screen", it includes the possibility to detach your jobs
from the terminal (^A-d), then logout, and after relogging in, just
type "screen -r", and voila: All still running, even the jobs which
tried to read somthing. Works fine under SunOS, Ultrix, SysV.4, the
only thing you need are sockets.

---------------------------------------------------------------------
>From bernie@metapro.dialix.oz.au Fri Jun 12 11:38:29 1992
Newsgroups: comp.unix.questions,comp.unix.shell,comp.sys.sun.admin

Sounds like you can start with the following bit of code I
use in my C programs to create daemons: (I assume that you
have the source code for the ptogram you are using.)

The code will create a child process, detached from the
terminal, running in background.

#!/bin/sh
# This is a shar archive.
# Manifest: daemon.c

if [ -f daemon.c ] ; then
  echo daemon.c exists. won\'t clobber
else
  echo extracting daemon.c
  sed 's/^X//' >daemon.c <<'END_OF_FILE'
X/*
X * change to "/" directory, put process in background,
X * and protect from signals before disconnecting from terminal
X */
X
X#include <stdio.h>
X#include <signal.h>
X
Xvoid daemon (prog)
Xchar *prog;
X{
X void (*hup_func)(), (*int_func)(), (*quit_func)();
X
X /* root directory so filesystems can be unmounted */
X if( chdir("/") == -1 ) { perror( "/" ); exit(1); }
X
X /* get a child process, which will become process group leader */
X switch ( fork() ) {
X case -1:perror ( prog ); exit(1); break;
X case 0: break;
X default:sleep(1); exit(0);
X } /* end switch */
X
X setpgrp(); /* become process group leader */
X
X /* ignore common signals */
X (void)alarm(0);
X if (( ( hup_func = signal(SIGHUP,SIG_IGN) ) == SIG_ERR ) ||
X ( ( int_func = signal(SIGINT,SIG_IGN) ) == SIG_ERR ) ||
X ( ( quit_func = signal(SIGQUIT,SIG_IGN) ) == SIG_ERR ) ) {
X fprintf ( stderr,"Failed to ignore signals\n" );
X exit(1);
X }
X
X /* close files in background */
X if ( isatty(fileno(stdin)) ) fclose(stdin);
X if ( isatty(fileno(stdout)) ) fclose(stdout);
X /* if ( isatty(2) ) close(2); this should be pointing elsewhere already */
X
X switch ( fork() ) {
X case -1:exit(1); break;
X case 0: break;
X default:exit(0);
X }
X
X setpgrp(); /* become process group leader */
X
X /* spawn a child, which will do the real work */
X switch ( fork() ) {
X case -1:exit(1); break;
X case 0: break;
X default:exit(0);
X }
X
X /* reset common signals */
X if (( signal(SIGHUP,hup_func) == SIG_ERR ) ||
X ( signal(SIGINT,int_func) == SIG_ERR ) ||
X ( signal(SIGQUIT,quit_func) == SIG_ERR ) ) {
X exit(1);
X }
X
X}
END_OF_FILE

  if [ $? -eq 0 ] ; then
    bytes=`wc -c < daemon.c`
    if [ $bytes -eq 1565 ] ; then
      echo "daemon.c extracted\n"
    else
      echo "daemon.c extract size error: was 1565, now $bytes bytes\n"
    fi
  else
        echo "extract daemon.c failed"
  fi
fi

# END OF ARCHIVE
exit

-- 
---------------------------------------------------------------------

From: parry@yoyo.cc.monash.edu.au (Tom James Parry)

In csh all you have to do is hit ^Z to suspend the job, then enter bg to put the process in the background, log out and go home. The program will run fine until it tries to read from the keyboard.

nohup is for sh only. csh does not require it (as I understand things).

--------------------------------------------------------------------- From: Julian Dermoudy <dermoudy@bruny.cc.utas.edu.au>

If I understand you correctly, the following should do the trick:

/usr/bin/nohup <cmd> &

Note though that since you're running under the C shell, nohup isn't required: the C shell automatically makes background jobs immune to HUPs.

You could just use:

<cmd> &

See nohup(1) for more details.

--------------------------------------------------------------------- From: vogel@c-17igp.wpafb.af.mil (Contr Karl Vogel)

I don't know much about Suns, but if you have the "stty" program, try the command

stty nohang

before you log off. I run on a Pyramid MIServer under the Berkeley universe, and it works like a charm. Hope this helps.

--------------------------------------------------------------------- From: miguel@roxanne.nuclecu.unam.mx (Miguel de Icaza A.)

If you're using C-Shell is very easy to leave a process in the background, you just have to type Control-Z (it may be diferent in your system), this will stop the Job, to leave it in the background, you'll have to type also:

bg <enter>

the bg command puts the current process (the one you stopped) in background, C-Shell auto-magicali prevent the task from getting a HUP signal.

--------------------------------------------------------------------- From: R.J. Stone <stone@athena.cs.uga.edu>

I'm not sure exactly what you mean by it's not doing what you want, but I thought I'd relate something that got me when I first tried to use this command.

When you do nohup, the process is immune to "hangup" signals (usually caused by "hanging up" the terminal). The shell that it runs under, however, isn't. So, you might be able to do "nohup xxyyzz" type "jobs", see "xxyyzz" on the joblist, but if you logout and then log back on, it won't be there. In addition, if you type "ps" or even "ps -a" it still won't show. It is there, however, but not attached to a terminal. Typing "ps -x" should show a running "xxyyzz" (unless it has exited.)

Perhaps what you ment is that you wanted to be able to reattach this to your terminal after you log back on (which I don't think this will let you do.) If you find a way to do this, please relate it to me!

Sorry if this doesn't contain any useful info. I'm still learning stuff like this so I try to help others out when I can since I can easily relate to their situation.

R/s --------------------------------------------------------------------- From: Paul Zhu <paulzhu@ruby.ils.unc.edu>

If you are using Bourne sheel, use $ (nice nohup your-command) &

if you are using c shell, Ctrl-Z and then 'bg' will put it in background without stoping even you log out. Good luck. --------------------------------------------------------------------- From: Georgios Dimitriadis <dimitria@csd.uch.gr>

[George also told me about Csh behaviour of ignoring HUPs, in Greek].

--------------------------------------------------------------------- From: Jerry Rocteur <jerry@incc.com>

Tikanis Dimitris,

I see no reason why the nohup command on your machine is not working, however, run the batch command instead:

batch <cr> command .... command (as many as you wish)

^D

then you can log off, any output from the program will be mailed to you.

Or batch<script

BTW I am sorry if this does not work on a sun, unfortunately, I have never had the pleasure of unsing one. --------------------------------------------------------------------- From: glover@skorpio.usask.ca (Ken Glover)

once the job is running, Control-Z bg

This will put it in the background, then you can log off.

If you are doing ftp's like this, you should look at a program called batchftp, available from an ftp site near you.

Ken --------------------------------------------------------------------- From: landman@hal.physics.wayne.edu (Joseph I. Landman)

Dimitris:

create a simple shell file to execute your program, call it new.bat.

then type the following line to set it up in the background immune to logoffs

nohup sh new.bat &

you need the ampersand (&) . new.bat might contain lines like

cd MolecularDynamics MolecularDynamics.x < Input.file > Output.file

where MolecularDynamics.x is your executable binary image.

--------------------------------------------------------------------- To: dimitris@aliakmon.cperi.forth.gr

Well, the simplest thing I can come up with uses sh. If your using csh, you'll have to call sh (unless csh has something similar). Try:

% sh -c "trap '' 1;do_whatever"

This just tells sh to ignore all HUP's (sig #1) then call do_whatever.

Of course you can make your own 'nohup'-type thing. Here are the basics:

#include <signal.h> main(ac,av) int ac; char *av[]; { signal(SIGHUP,SIG_IGN); execvp(av[1],av+1); }

This will let you type something like: a.out whatever and its args And it'll ignore hups. ---------------------------------------------------------------------



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:06:43 CDT