1

Possible Duplicate:
How to suspend and bring a background process to foreground

I have kind of an odd problem which I haven't encountered before. Here's what I did:

I started an application with ./myApp.

No command line arguments, no & to make it run in background.

Unfortunately, my system froze at some point. No access via the GUI was possible, so I decided to first stop all CPU-intensive tasks. So I brought up a tty (Shortcut Ctrl + Alt + F2 on Ubuntu) and did kill -STOP pid, after getting the right pid of myApp with top.

Luckily, after some extra time, the system became unfrozen. So I restarted the process with kill -CONT pid.

But here's what I see in the terminal:

[2]+ Stopped  ./myApp

And now the terminal is in the state as if the application isn't running (i.e. I can do ls or whatever I like), but the process manager still reports the application to be running. I'm very glad it does, because I would lose 34 hours of computing time (and yes, it's an extremely bad app; not written by me, of course), since there is no output at all to file.

When the heavy task of the application is finished (expectedly in a couple of hours), I need to enter a specific task inside of the application to make it write to file (it is somewhat of a custom shell prompting for input, so I guess cin is in use).

So the question is: How to bring me back to the application such that I see its output and can interact with it? If not possible: is it possible to stream into the app and initiate the required command without restarting the app?

Update The application does still output to the exact same terminal.

FYI, as you might need it: I'm running Ubuntu x86 with kernel 3.2.0-36

stefan
  • 1,009
  • I found this question http://unix.stackexchange.com/questions/45025/how-to-suspend-and-bring-a-background-process-to-foreground suggesting to use fg to bring back background processes. Might that work in my case? – stefan Jan 07 '13 at 23:45
  • Yes indeed. Pressing Ctrl+Z has the same effect as sending a STOP signal: the application is suspended. You can run fg to bring it back to the foreground, or bg to run it in the background (where it can consume CPU time but not read input). – Gilles 'SO- stop being evil' Jan 08 '13 at 00:06
  • @Gilles & jasonwryan: Thanks for improving my question :) – stefan Jan 08 '13 at 00:07
  • @Gilles: That's perfect. It seems to work (though the app is busy calculating something, so I might not know 100% until tomorrow). What do you think of closing this as duplicate? – stefan Jan 08 '13 at 00:29
  • I already cast the first close vote. – Gilles 'SO- stop being evil' Jan 08 '13 at 00:34

1 Answers1

0

I don't believe it is possible to reattach a console to a process which has no associated psuedo-tty.

For future scenarios where maintaining a connection to a console is required, try using screen.

Screen is a tool designed for interfacing between a specific instance of a shell and the local terminal. By being a man-in-the-middle, you can detach from the running process and reconnect to it later. This is useful if you have an unreliable Internet connections if you are using SSH for system management or in instances like yours with a long-running process.

When you open up your terminal (whether in the GUI or on one of the real ttys), just type screen. It should be installed by default in Ubuntu. Screen will remind you that it is running your default shell and how to find help. It will then run your login shell and stay back out of the way.

At its most basic usage, you can use Ctrl-A, followed by D to detach from the current session. To reconnect to that session, use: screen -r <complete name of session>. If you cannot recall the session name, the -ls command switch will provide details as to which sessions are available.

Screen can do so much more that I haven't had much opportunity to use, but years ago, the Digium support staff helped me configure one of their analog POTS interface cards using a reverse SSH connection and Screen in multi-user mode. This gave them root access to my Asterisk server securely without a need for them to know the password and with the ability for me to audit what was being done.

  • any ideas of piping or something like that? I basically only need one command executed. Not seeing the app is totally fine. – stefan Jan 07 '13 at 22:51
  • @stefan I second the recommendation of using screen for future runs. For this time, while it is possible to reattach your program to another terminal (e.g. inside a new Screen session), this is risky — it could kill your application. So don't try it out when the calculation is almost over. – Gilles 'SO- stop being evil' Jan 08 '13 at 00:08
  • @Gilles: Fortunately, this will probably be the last time that I have to use this app (I've got the job to build a new version from scratch). But of course, screen would be helpful in other situations as well. – stefan Jan 08 '13 at 00:28