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?

6
  • Just a guess but did you try ctrl+z or running your command like this; #command &
    – EralpB
    Dec 3, 2012 at 2: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 3, 2012 at 2: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 3, 2012 at 2:56
  • The accepted answerer on this question explains the three steps which needs to be taken: stackoverflow.com/questions/625409/… Aug 13, 2014 at 7: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 4, 2016 at 19:11

1 Answer 1

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.

5
  • i tried it but as its outputing something , it again come at foreground with% showing how much data is uploaded
    – Mirage
    Dec 3, 2012 at 4: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 3, 2012 at 4:48
  • so it means , if the process is already started then there is no way redirect the ouput
    – Mirage
    Dec 3, 2012 at 4:54
  • Yes indeed. You cannot effect the process only the shell that it is running from. You could do a kill -9 <pid of your shell and then the process will still run and output will be thrown away - but you will have to login again to start a new shell.
    – Ed Heal
    Dec 3, 2012 at 5: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. Nov 13, 2017 at 16:09

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