0

At work we have a supercomputer accessible via SSH.

We login, reserve the cores using bsub command with parameters, and the latter creates (from doc) "an interactive bash session with pseudo-terminal."

In that "pseudo-terminal" we then go to a folder, start the solver with parameters and let it run to completion.

So the app completes and the session stays open, and the reserved cores remain reserved, then a couple days later (if the sim finished Friday after work) the IT sends an email on Monday stating that the cores are reserved, but they see no activity, blah blah blah...

I've checked https://stackoverflow.com/questions/14254118/waiting-for-background-processes-to-finish-before-exiting-script and Bash - how to run a command after the previous finished? and other forums/tutorials, but all I find is how to keep an application running after closing the terminal (detaching the application from a terminal).

I need the opposite, I want to close the terminal (this can be the last step of the solver, so it could exit the terminal and be killed)

I am open to suggestions, I can do it in a script, or I can get the guy to code it in his C++ solver.

I tried the following (on my Ubuntu Server running in a VM, expect this to work more or less the same in any bash environment, please correct me if I'm wrong):

delay_test.c:

#include <stdio.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
    printf("Doing some fake lengthy operation...");
    sleep(15); // sleep for 15 seconds
    return 0;
}

Compiles and runs as expected. I tried system("clear"); (part of stdlib.h) before returning, and that works too, I get the message, then 15 seconds later the screen is cleared and the app exits.

Then I tried system("exit"); and nothing happens (I mean the app exits as if that exit statement wasn't there...). My best guess is that the app has to return before one can exit the terminal, but it's only a guess.

I also tried a script way:

test.sh:

#!/bin/bash
./delay_test &
wait
exit

In this case, my understanding tells me that the exit statement is to finish the script, but I need to exit out of the terminal session once the app in question terminates.

So what can I do to force the "pseudo-terminal" to exit after the app exits?

nurchi
  • 146
  • 4
    Couldn't you just exec whateveryourappis in the shell to replace the shell with that app that will exit (and thus close the ptty) when it exits? – thrig Oct 28 '16 at 17:01
  • When I reserve the cores, the shell is created for me. When I exit that shell, the cores are freed. I have no control over what the shell is, it is launched for me at the same time the cores are reserved. We are not the only ones using the supercomputer, so the sub-shell is a better option to accommodate the needs of most. Finally, we supply the solver, so I don't want the login to depend on the solver's app name (besides, the parameters can change too, these come in form of an SQLite database, filename is different for every simulation). – nurchi Oct 28 '16 at 17:04
  • 1
    Again, why can't you just exec thesolver to replace the provided shell with whatever your solver is? – thrig Oct 28 '16 at 17:17
  • Unless I misunderstand your comment, I don't run the shell directly, I run bsub with some parameters to reserve the cores on a supercomputer and it creates a bash shell for me, I don't control this process. In that sub-shell I run our solver. – nurchi Oct 28 '16 at 17:30
  • Oh, wait, sorry. I didn't know what exec did. Sorry. I just tried it in my local VM, works like a charm. Please answer this question (vs comment) so I can mark it appropriately. Thanks. – nurchi Oct 28 '16 at 17:31
  • 2
    I kept on looking and found another solution that also worked. From http://askubuntu.com/questions/611648/exit-terminal-after-running-a-bash-script, command -parameters ;exit worked as well. – nurchi Oct 28 '16 at 17:34

1 Answers1

1

The most straightforward way in the shell provided by the bsub command is to prefix whatever command you are to run with exec, which is a shell built-in that will replace that provided shell with your solver:

exec yoursolver

When yoursolver exits, that will return to bsub, as the shell has long since been replaced by the solver.

(As an aside, note that some languages (TCL, PHP) have an exec that is not at all like the shell or Perl exec or the C exec(3) functions, so if you're using or familiar with those languages beware this difference; the TCL and PHP versions are more akin to the C system(3) or Perl system function or typing the commands into a shell in that the original process is still around afterwards.)

thrig
  • 34,938
  • This is why I was confused when you suggested using exec, I had never used it before and thought this was (as you said) similar to C system()... – nurchi Oct 28 '16 at 18:37