2

Not sure how to google this (I tried with some things and didn't get much) or how this is even called. This is my typical workflow:

  1. SSH to a server
  2. run something there and leave it running in the background (bg command for example)
  3. exit SSH and come back later

Is there any way I could, when I come back later, see if the process was killed and on what status it was killed (segfault or something else) or get any kind of exit status information of the process I was running? For example, if I run it normally and leave my terminal open, it will print segmentation fault or something else, but I have no idea how to obtain this status once I disconnected and connected back again.

3 Answers 3

1

Use screen or tmux to make your interactive session persist after you've disconnected.

For screen, you can detach the session with Ctrla followed by d. You'll see a message like

[detached from 12345.pts-9.hostname]

You can then safely disconnect. When you reconnect, screen -r will put you right back where you were.

For tmux, use Ctrlb then d to detach, and tmux attach to reattach.

1

use # screen command for background process you can easily check the background process in screen for more information refer this site -> http://www.tecmint.com/screen-command-examples-to-manage-linux-terminals/

1

One way is simply to redirect the output of the program to a file. In a bash shell you could do this:

{ myprog; echo "Exit status: $?"; } </dev/null &>output_file & disown

Note that if you are going to exit the ssh session, you need to make sure you disconnect the process from the terminal by redirecting all out put as above.

If you want to avoid logging back in again to check the output of the command, one option is to have the server email the output to you (provided it is properly set up to do so). Creating function in your .bashrc on the server might be an easier way if you have to repeat this often:

run_and_email () {
  {
    {
      "$@"
      echo "Exit status: $?"
    } 2>&1 |
    mail -s "$1 output" you@example.com
  } </dev/null &>/dev/null & disown
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.