3

I'm really new to linux (like earlier this week is when I started) so excuse me if this is a really simple question. I am running a program on a remote server and it's a lengthy process dealing with a couple hundred gb of data so I wanted to leave it running overnight. Long story short I ssh in, I start the program, watch to see that it's running and then close out of terminal. I come back this morning and see it stopped exactly where it was when I terminated my ssh the night before. Is there a way to keep the process running on the server when I close out?

11

As the other answers suggest. You can use nohup <command> &.

You can also use screen, this (basically) is a detachable terminal. You can start a terminal using the command screen and when you want to detach from it. Ctrl+ad. And to reattach your terminal run screen -r <terminal_name>. If you have more then one detached terminal's, you can use the command screen -r to see the names of the detached terminal's.

Screen is a full-screen window manager that multiplexes a physical ter‐ minal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.

EDIT:

You can also use tmux. Have a look here for a basic "how to use tmux".

tmux is a terminal multiplexer. What is a terminal multiplexer? It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. And do a lot more.

  • 1
    ctrl+a, d rather than ctrl+a+d – Gnudiff Sep 7 '17 at 14:34
  • @kusalananda Does 'nohup' keep the process running even when I log out from the remote server? I think I tried this, and when logged in again, the 'jobs' command didn't work. I'll give it one more try. – mithusengupta123 Apr 13 '18 at 10:11
  • @mithusengupta123 The jobs command will only list jobs started in the current shell. If you start a job with nohup and then log out, then the jobs command would not list them (but the may still be running). I would suggest using screen instead, it's a lot more versatile. – Kusalananda Apr 13 '18 at 10:14
  • @Kusalananda When a job is done, will 'top' show it? – mithusengupta123 Apr 13 '18 at 11:58
  • @mithusengupta123 No. – Kusalananda Apr 13 '18 at 12:09
2

The command you want is called nohup. Basic syntax is:

nohup <command>

This will run <command> and forcibly ignore SIGHUP (the signal sent to processes when their controlling terminal is closed, such as when the parent SSH session ends). Note that it is possible on a Systemd based system to prevent this from working, but no distro I know of uses such a configuration by default.

Alternatively, check if the command at is present and atd is running. If both are the case, you can tell the system to run your script as a batch job and possibly even e-mail you the output by using the following:

at "`date +%FT%T`" << EOF
<command>
EOF

You can substitute the contents of the double quotes for any reasonable date and time specification (at is smart enough to handle reasonably human readable stuff like '8AM tomorrow' or 'next sunday'), but the given command will start the job immediately (the date command will return the current date and time as an ISO 8601 timestamp).

0

Login to your server over SSH and run:

nohup YOUR_PROGRAM &

This will run your program in background and running process won't stop if your SSH session will disconnect and stop shell you were using.

Check out this thread for details about nohup and &.

0

You can use nohup in this case.

Try

$ nohup ./YourExecutableFile &

This will run your executable file and put it in the background and it wont stop even if you logout.

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.