48

I am using putty to interact with Linux server. I have started a process using putty. The process is running and will take 5-6 hours. I want that process to keep running after I close the putty session. How can I keep that process alive after closing the putty session? I do not want to keep the computer ON all the time. Is there any way to do this?.

Prakash Panjwani
  • 637
  • 2
  • 6
  • 7

10 Answers10

37
^Z
bg %1
disown -h %1

The '-h' makes the process immune to SIGHUP when the session completes.

Tom Anderson
  • 1,743
  • 1
  • 13
  • 13
  • 4
    Note that there's no way to 'reown' or 'adopt' the process - once you've done this, you can't ever bring it back to the foreground. If the process is a batch job working to produce some files, though, that shouldn't matter. –  Jan 16 '10 at 15:34
  • ...but after running `bg %1` the job is scrolling down the page again (in my case an rsync). So how can I then input `disown -h %1`? – Codemonkey Aug 03 '21 at 22:28
  • Though just `bg %1` seems to do the job, it certainly is still running after closing the terminal. What's the point of the `disown` line? – Codemonkey Aug 04 '21 at 07:01
  • @Codemonkey You can type input into the shell even when a background job is printing output. It will look weird, but it will work! – Tom Anderson Aug 06 '21 at 11:25
  • @Codemonkey When a terminal is closed, it sends a 'hang up' signal (SIGHUP) to the shell, which sends it on to background jobs. Default behaviour is for programs to terminate when they receive a SIGHUP. `disown -h` tells the shell not to forward the SIGHUP. If you do not need the disown, that might be something to do with your terminal or shell setup, so it does not send SIGHUP, or might be because the program ignores SIGHUP. – Tom Anderson Aug 06 '21 at 11:27
34

I use screen for that kind of stuff. Actually sometimes I just leave it on quite a while so I can get back to what I was doing.

Update 2021: I also started using tmux lately.

mtmk
  • 601
  • 4
  • 7
  • The process is running now ,is it work for that process?I am asking because the process is on the middle and i do not want to restart it now –  Jan 16 '10 at 15:18
  • +1 for mentioning my undisputed favourite command line tool. It's a gem in situations like this. – Noufal Ibrahim Jan 16 '10 at 15:41
  • @Noufal, totally! @prakash, you need to run screen before you can start any processes – mtmk Jan 16 '10 at 15:48
  • If you want to connect to several machines it can even be a nice idea to run several screens inside the first screen so you just have to connect to one screen to get access to all your running remotes. –  Jan 16 '10 at 17:26
  • the link does not work anymore – Malky.Kid Nov 16 '18 at 22:46
22

Use the nohup command. Just prefix it to your command and it will daemonise them so that they won't stop when you log off/terminate your shell session. The standard output will by default be in a file called nohup.out. Check the manual page for nohup(1) for more information.

Noufal Ibrahim
  • 689
  • 4
  • 16
  • the process is running ,is it work for that process?I am asking because the process is on the middle and i do not want to restart it now? –  Jan 16 '10 at 15:16
  • Too late...at least, for normal operations. It wouldn't be altogether surprising to find that if you attach a debugger to the process, and then tweaked it with an appropriate set of system calls, then perhaps you could put it in the background. But it is not what could be recommended - I wouldn't bother, for example, and I have at least some idea of what might be involved. You have two choices now...either stay connected until it finishes, or stop the current process, and restart a new one using nohup. – Jonathan Leffler Jan 16 '10 at 15:19
  • 1
    If you're using bash (and I think zsh), you can use Tom Anderson's solution and `disown` the process so that it continues to run even when you disconnect. – Noufal Ibrahim Jan 16 '10 at 15:40
  • Yup - disown looks to be the way to go for an already running job. It's doable because the shell that started the job can manipulate the properties needed. – Jonathan Leffler Jan 16 '10 at 15:56
5

Start process with nohup "processname" &. You can also detach it with screen or tmux.

Excellll
  • 12,627
  • 11
  • 51
  • 78
Damir
  • 141
  • 1
  • 3
5

The above solutions are quite well described, however, none of them worked for me UNTIL I also edited PuTTY configuration to :

Enable TCP keepalives (SO_KEEPALIVE option)

I hadn't seen this anywhere else, and just found it by trial and error.

user295374
  • 51
  • 1
  • 1
1

if the process is a for nodejs, and it may be your intention since you originally posted this on stackoverflow. I was originally searching for this question myself. I found pm2 and it is amazing. The other answers may help for general putty but if it is node specific this is by far the best answer, as there is built in monitoring and the setup is simply a

$ npm install pm2 -g
$ cd yourappdirectory

"PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Starting an application in production mode is as easy as:"

$ pm2 start yourappname.js

"using the save and then freeze command you enable the processes to autostart on reboot"

$ pm2 save
$ pm2 freeze

for monitoring its

$ pm2 monit

and restarts

$ pm2 restart yourappname

also for direct logging information

$ pm2 logs

I now can easily run two putty windows instead of using my digialocean browser window (which I couldnt scroll up on); For more info see the main github

https://github.com/Unitech/pm2

its amazing.

1

Ctrl+z Send the current process to the background.

Also, you may add & at the end of your command to run in in background

BarsMonster
  • 140
  • 2
  • 4
  • 18
  • 4
    Normally, control-z puts the process into a state of suspended animation; the bg command puts it into the background. However, that also keeps the process attached to the terminal, whereas nohup detaches it from the terminal and therefore allows putty to disconnect and the process to survive the disconnection. – Jonathan Leffler Jan 16 '10 at 15:15
  • I found that using '&' to send to the background works with putty, but when I connect using directly ssh from a shell, if I close the connexion, the background process is killed. Any idea of why ? – Zardoz89 Nov 19 '15 at 08:36
0

How to keep weblogic running after closing putty window:

Simple Steps: after the login through putty follow the below steps:

  1. Go to the directory on the server where the startWebLogic.sh command is located.
  2. Type command screen and press Enter (a new screen will open).
  3. In the new screen type your run command ./startWebLogic.sh.
  4. Press Ctrl+a then press d (without holding Ctrl); you will return back to the previous screen.
  5. When you want to return to your server log screen, type command screen -r.
Toby Speight
  • 4,866
  • 1
  • 26
  • 36
Akbar
  • 1
0

You can use & after the link.

Example:

[localhost ~ ]# wget http://www.link.com/download/download.zip &
Excellll
  • 12,627
  • 11
  • 51
  • 78
codemania
  • 111
  • 2
0

If you want the program that is contained in the process to always or frequently run in the background, you can code it to separate from the controlling terminal (make such behaviour controllable via an option flag) and run in the background.

That's a long term solution, of course, not for the currently running process.

mpez0
  • 2,792
  • 1
  • 16
  • 20