5

I have written a program which I run after connecting to the box over SSH. It has some user interaction such as selecting options after being prompted, and usually I wait for the processes it carries out to finish before logging out which closes the terminal and ends the program. But now the process is quite lengthy and I don't want to wait whilst being logged in, so how could I implement a workaround for this in C please?

user1166981
  • 1,738
  • 8
  • 26
  • 44

6 Answers6

7

You can run a program in the background by following the command with "&"

wget -m www.google.com &

Or, you could use the "screen" program, that allows you to attach-deattach sessions

screen wget -m www.google.com

(PRESS CTRL+D)

screen -r (TO RE ATTACH)

http://linux.die.net/man/1/screen

Seidr
  • 4,946
  • 3
  • 27
  • 39
  • So if I were to run the program like ./program & I could still interact with the program like normal, but when I close the terminal its still runnin in background even after logoff? – user1166981 Jan 27 '12 at 16:46
  • 3
    No, if you run the program with "&" it will be knocked into the background, but still output to the terminal - however you will be unable to interact with it. It will continue to function after closing the terminal. For what you're describing, I'd definately suggest using "screen" - it's fantastically useful. – Seidr Jan 27 '12 at 16:47
  • So you mean it can't take user input but you can have it report what its doing, for example? – user1166981 Jan 27 '12 at 16:49
  • 1
    Exactly - with screen you can interact with it as normal..think of it as a mimized window in GUI terms. You can get back to it by re attaching, and it will continue to run in the background, even if you end your session (as long as you detatched it). – Seidr Jan 27 '12 at 16:51
  • When I close the terminal my program is also close. – Damjan Pavlica Jan 02 '15 at 22:56
  • That's correct Daman Daman. If you need to have it continue to run after the terminal has been closed, I'd recommend using screen, as described above. – Seidr Jan 03 '15 at 07:23
3

The process is sent the HUP signal when the shell exits. All you have to do is install a signal handler that ignores SIGHUP.

Or just run the program using nohup.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • How you run the program using nohup? Does that have any negative implications for the interaction with the program within the shell? Thank you – user1166981 Jan 27 '12 at 16:46
  • The syntax would be "nohup command arguments &". Implications include the program output being written to a file called nohup.out -unless you redirect it to a different file- and that stdin is not connected to the terminal. – Joni Jan 27 '12 at 16:52
  • 1
    Oh, but your program is interactive. In this case you need screen. – Joni Jan 27 '12 at 16:56
2

The traditional way to do this is using the nohup(1) command:

nohup mycmd < /dev/null >& output.log &

Of course if you don't care about the output you can send it to /dev/null too, or you could take input from a file if you wanted.

Doing it this way will protect your process from a SIGHUP that would normally cause it to exit. You'll also want to redirect stdin/stdout/stderr like above, as you'll be ending your ssh session.

Syntax shown above is for bash.

FatalError
  • 52,695
  • 14
  • 99
  • 116
  • Unfortunately, you won't be able to interact with the program. – BRPocock Jan 27 '12 at 17:10
  • That's true -- I missed that part of the question initially. However, if the interaction happens in a predictable way, then the user input could be put in a file and the file redirected on stdin. – FatalError Jan 27 '12 at 18:11
1

you can use screen command. here is a tutorial. note you might need to install it to your systems.

mantrid
  • 2,830
  • 21
  • 18
0

There are many options :-) TIMTOWTDI… However, for your purposes, you might look into running a command-line utility such as dtach or GNU screen.

If you actually want to implement something in C, you could re-invent that wheel, but from your description of the problem, I doubt it should be necessary…

BRPocock
  • 13,638
  • 3
  • 31
  • 50
  • In all honesty I would love to implement the solution from C rather than relying on the existing programs on the box. – user1166981 Jan 27 '12 at 17:34
0

The actual C code to background a process is trivial:

//do interactive stuff...  
if(fork())
     exit(0);
//cool, I've been daemonized.

If you know the code will never wind up on a non-linux-or-BSD machine, you could even use daemon()

//interactive...
daemon(0, 0);
//background...
Dave
  • 10,964
  • 3
  • 32
  • 54