131

I have a command that uploads files using git to a remote server from the Linux shell and it will take many hours to finish.

How can I put that running program in background? So that I can still work on shell and that process also gets completed?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Mirage
  • 30,868
  • 62
  • 166
  • 261
  • Just a guess but did you try ctrl+z or running your command like this; #command & – EralpB Dec 03 '12 at 02:44
  • The command is already running so i dont have other option. I am not sure which command to try. i didn't wanted to break the current process so i didn't experimented it – Mirage Dec 03 '12 at 02:54
  • We should wait a more professional answer then :) I meant if you had the chance to start all over again. ( The command & thing) – EralpB Dec 03 '12 at 02:56
  • The accepted answerer on this question explains the three steps which needs to be taken: http://stackoverflow.com/questions/625409/how-do-i-put-an-already-running-process-under-nohup – Leonard Saers Aug 13 '14 at 07:40
  • You can also just open a second instance of putty and connect to the server again to get another shell. Though the solution with `ctrl+z` is great. – Ela782 Jan 04 '16 at 19:11
  • try something like this : `myapp > /tmp/myapp.log &` this will run `myapp` in background and its output will be written to `/tmp/myapp.log` – user889030 Jun 03 '20 at 09:04

1 Answers1

242

Suspend the process with CTRL+Z then use the command bg to resume it in background. For example:

sleep 60
^Z  #Suspend character shown after hitting CTRL+Z
[1]+  Stopped  sleep 60  #Message showing stopped process info
bg  #Resume current job (last job stopped)

More about job control and bg usage in bash manual page:

JOB CONTROL
Typing the suspend character (typically ^Z, Control-Z) while a process is running causes that process to be stopped and returns control to bash. [...] The user may then manipulate the state of this job, using the bg command to continue it in the background, [...]. A ^Z takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded.

bg [jobspec ...]
Resume each suspended job jobspec in the background, as if it had been started with &. If jobspec is not present, the shell's notion of the current job is used.

EDIT

To start a process where you can even kill the terminal and it still carries on running

nohup [command] [-args] > [filename] 2>&1 &

e.g.

nohup /home/edheal/myprog -arg1 -arg2 > /home/edheal/output.txt 2>&1 &

To just ignore the output (not very wise) change the filename to /dev/null

To get the error message set to a different file change the &1 to a filename.

In addition: You can use the jobs command to see an indexed list of those backgrounded processes. And you can kill a backgrounded process by running kill %1 or kill %2 with the number being the index of the process.

Community
  • 1
  • 1
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • i tried it but as its outputing something , it again come at foreground with% showing how much data is uploaded – Mirage Dec 03 '12 at 04:37
  • The process will write any output to the terminal as you instructed the process to do that at the start. You will have to restart it to get output to write to file or `/dev/null`. See edit above. – Ed Heal Dec 03 '12 at 04:48
  • so it means , if the process is already started then there is no way redirect the ouput – Mirage Dec 03 '12 at 04:54
  • Yes indeed. You cannot effect the process only the shell that it is running from. You could do a `kill -9 – Ed Heal Dec 03 '12 at 05:01
  • You could also use the `disown` command if you need to log out of your terminal session, and you would like it to continue running. – kirk roerig Nov 13 '17 at 16:09