How to bring an Orphaned Background Process back to Foreground? - shell

Well it goes like this, I had to run a program from my home by sshing into the server in my institution. I did not want my program to be terminated when the session closes(I didn't know about screen).
What i did was press Ctrl+Z and then type bg so that it executes in the background.
The session got terminated. Now when I login from my institution machine and type ps -u username, it shows that the program is still running but I'm unable to bring it to foreground.
I tried fg and jobs but these commands don't give me any output.
Please someone help me..

If you have started the process without using "screen" command then you cannot take over that process. Basically you cannot take over a process that was started in a different shell.
When your session is terminated all the bg process will go the detached state. Though you might be able to see the details of such process you cannot fg them to a shell from login afterwards

If a process has been orphaned, you can't "reparent" it to a different shell and use fg, bg, ^Z, ^C, and so forth to control it.
It seems you're asking implicitly how to control an orphaned process. Since you can see the process using the ps command, you have its pid. You can use this pid as the argument to the kill command, which will allow you to stop, continue, or terminate the process. You can't wait for the process to finish, but you can poll to see whether it still exists by using the kill -0 <pid> command.

https://serverfault.com/questions/55880/moving-an-already-running-process-to-screen
Gives an alternative view on this question, The top answer suggests using Reptyr.

Related

Windows: Can the return value of a process running C++ code be accessed if you don't run from command line?

I know that you can access the return value of the process using the command line or by having one process create and run the other. However, if I just make an *.exe and double click it, does the return value go anywhere that I can access? If so, where? Could I change any settings so that, if my process returns EXIT_FAILURE, Windows will handle things differently than if it returns EXIT_SUCCESS?
No, I don't think anything retains the exit value of a process started in that way. When you double click on a shortcut or executable, Explorer creates the process and then immediately closes the handles because it no longer cares what happens.
You could write a program that calls OpenProcess on the process of interest while it's running. (It would have to have a way to discover the process ID before the process exits.) OpenProcess will give you a handle to the process. The program could then wait on that handle. When the process exits, the program could use the handle to retrieve the status code and do whatever it is you want it to do.

Close all open X-windows with a command in UNIX

I have variable stand alone shell routines which I execute one after another automatically. Since every routine produces several X-windows figures I want to close all of them at the end without modifying every single routine. Is there a certain command?
Cheers!
Based on answers to this question. xdotool looks like what you need.
To kill an X11 window given it's title, you can use:
xdotool search "Your window title here" windowkill
Windows are owned by processes. One way to close windows is to kill their owner processes.
Another way is to start another X server, run your scripts with DISPLAY environment variable referring to the display of that X server, then terminate that X server with all windows.

Run script in background?

Simple question: Is there a way to run a script in the background with out terminal running?
More detail and background: I had an app that read an apps .log file and puled information from it, then provide information and statistics from the information in the log.
An update to the app changed the way the .log file was written and delete information and duplicates the log in a manner that i have been unable to predict.
the app that was designed to interface with the log was not coded to check for such changes. so when it attempts to gather information after the log change it stops working.
A "hack" has been devised to run a tail -f, then hexed the app to point at the new file.
(The "hack" works)
I would like to run the tail in the background so that the user doesn't interrupt it... breaking it...
-sorry for the (possibly) longer than needed description. BUt i figured a more detailed question would get me a precise answer.
Thanks in advance!
~¥oseph
The answer depends on if you need to be able to re-connect to the process after exiting the shell. If the process is non-interactive and can simply be left alone, then "nohup program &" should do the trick. But that won't let you continue to interact with the program after you've closed the shell.
If it's a interactive program, then your best bet is to use screen or one of the other terminal-multiplexers. You start "screen" which gives you a new shell, in this you start whatever program you want, the usual way, say "nano myfile.txt".
When you want to close the shell, but leave the program running, you press C-a d ('Detach') to detach from screen. it keeps running, but in the background, and will keep running even if you log out.
When you then later want to reconnect to screen you open a new shell and type "screen -r" (reconnect), this leaves you right where you where.
Screen also lets you run several different shells in a single terminal-window and is a neat tool overall. Check it out.

Suspending a process in bash running inside console2

In other terminals I would press Ctrl+z to suspend an application, often to then issue bg to send it into background.
In console2 Ctrl+z does nothing, probably because the key combination has a different meaning in Windows. But is there a way to achieve the same effect, save for 'process &'?
(I know I should use & and it works, but sometimes I would setup shell, start an editor, begin editing, then return to the console just to find that I forgot the & and I can't use the shell. It annoys me that I then have to either open a new shell and set it up again, or quit the editor, start it with & and set it up again).
In Console2, ctrl-c is bound by default to copy (text) you have to press ctrl-shift-c to cancel job.
You could also try to remove the hotkey that is conflicting with ctrl+z.
I was having trouble using ctrl+c to cancel the execution of a script. As soon as I removed that hotkey, ctrl+c had its default restored and I was able to cancel executions properly.
This annoying issue surely is a console2 bug. You may find something about at its issue tracker.
Anyway, what ctrl+z does is send a SIGSTOP signal to current process. So, you can still send that signal from another session/tab. (If it is less annoying than stop and start with &).
To do that, you can use the kill command.
kill -s SIGSTOP pid
(pid is the process PID number)
Hope it helps.

Can I Force MATLAB to quit after user presses Control-C?

I'm running MATLAB (command line version) from a shell script, and I'd like it to preserve shell behavior where if you press Ctrl-C it exits. But instead it wants to keep control of the terminal and I (or my poor users after me) have to type quit(1) to make it quit and tell the shell it failed.
You can't intercept Ctrl-C with a try/catch block... any other ideas? Anything I could do from the shell side to intercept the keystrokes before they get to MATLAB?
onCleanup seems like an option, but then I'd have to make the whole script thing into a function (it's already a dynamically generated try/catch block thing in a Makefile). But if that's the only thing that will work, then I'll do it...
Use onCleanup:
I wanted to do the same thing but after I read this thread I used onCleanup successfully. My problem was I had a server in Matlab that when pressing CTRL+C would keep listening on the port it was started on -> second run I would get a bind error.
You can try:
stty quit ^C
but I have no matlab to test it.

Resources