449

How can I resume a stopped job in Linux? I was using emacs and accidentally hit ctrl-z which blasted me back to the console. I can see it when I type 'jobs'

[*****]$ jobs
[1]+  Stopped                 emacs test_queue.cpp
1
  • 1
    This is actually a fairly normal work flow for Vim, if you want to keep you commands in your bash history, then you hit Ctrl-z type your commands and then resume. Obviously you can run commands without leaving Vim via the :! ed command
    – icc97
    Jul 11, 2018 at 2:32

6 Answers 6

520

The command fg is what you want to use. You can also give it a job number if there are more than one stopped jobs.

2
  • 79
    for reference, fg is "foreground". You can also continue the job in the background with "bg".
    – Sirex
    Apr 8, 2011 at 11:05
  • 4
    You can also give it a job number --> Simply doing fg 1 doesn't work, the syntax is fg %1
    – jaques-sam
    May 4, 2023 at 7:50
344

The general job control commands in Linux are:

  • jobs - list the current jobs
  • fg - resume the job that's next in the queue
  • fg %[number] - resume job [number]
  • bg - Push the next job in the queue into the background
  • bg %[number] - Push the job [number] into the background
  • kill %[number] - Kill the job numbered [number]
  • kill -[signal] %[number] - Send the signal [signal] to job number [number]
  • disown %[number] - disown the process(no more terminal will be owner), so command will be alive even after closing the terminal.

That's pretty much all of them. Note the % infront of the job number in the commands - this is what tells kill you're talking about jobs and not processes.

7
  • 49
    I avoid "kill %1" because mistyping it as "kill 1" is really really bad :)
    – user59328
    Apr 8, 2011 at 14:05
  • 8
    @barrycarter That's very true. I usually do an fg and a Ctrl-C ;)
    – Majenko
    Apr 8, 2011 at 14:08
  • 7
    @barry: Which is why init in Upstart ignores SIG{TERM,KILL} by default.
    – Hello71
    Apr 19, 2011 at 2:26
  • 7
    And, of course, "never run as root" ;)
    – user59328
    Apr 19, 2011 at 3:04
  • 1
    Why use "%" character. Is it required to be prepended before the job number or Is it a unix convention to specify the int type ? Feb 28, 2020 at 5:03
55

You can also type %<process_name>; i.e., you hit Ctrl-Z in emacs, then you can type %emacs in the console and bring it back to the foreground.

1
  • Very good to know
    – ZAD-Man
    Oct 22, 2015 at 21:42
37

Just to add to the other answers, bash lets you skip the fg if you specify a job number.

For example, these are equivalent and resume the latest job:

%
%%
fg
fg %

These resume job #4:

%4
fg 4
4
  • 3
    While this is kind of cool, I still find it easier to type fg than %.
    – rr-
    Oct 10, 2014 at 17:10
  • 4
    % is awesome, thanks! As a touch-typist, I find fg very irritating (same finger). But then, so is cd.
    – Gauthier
    Apr 15, 2015 at 13:45
  • 1
    And you can start it in the background with either bg % or just % &.
    – Wildcard
    Aug 16, 2016 at 18:43
  • in my case when i tried to use fg i see the stopped process appears and disappears quickly and just <fg %> succeeded to restore it.
    – Lafi
    Jun 25, 2019 at 12:54
33

If you didn't launch it from current terminal, use ps aux | grep <process name> to find the process number (pid), then resume it with:

kill -SIGCONT <pid>

(Despite the name, kill is simply a tool to send a signal to the process, allowing processes to communicate with each other. A "kill signal" is only one of many standard signals.)

Bonus tip: wrap the first character of the process name with [] to prevent the grep command itself appearing in the results. e.g. to find emacs process, use ps aux | grep [e]macs

4
  • 5
    This also works if you disown a stopped process
    – mabraham
    Dec 6, 2017 at 1:21
  • can you also get access to its input/output as it happens when you say fg ? Oct 2, 2018 at 9:39
  • This is a much more flexible approach than working with job number. Thumbs up.
    – thomp45793
    Feb 14, 2019 at 22:55
  • Shorter still: kill -SIGCONT $(pgrep <process name>)
    – trebor
    Feb 23, 2023 at 17:44
0

For anybody who wants to continue all suspended processes.

I don't know how but I achieved several times to completely lock my screen. When I check the processes there are dozens that are suspended. So I continued all processes to unlock my screen.

Switch to text console with Ctrl-Alt-F3 or similar. Log in, then issue:

ps ax|grep " T"| awk '{print $1}' |grep -v PID | xargs echo kill -SIGCONT
  • ps lists all processes
  • grep finds mostly only suspended processes (dirty hack, improve?)
  • awk fetches only first column, PID
  • grep removes header "PID"
  • xargs executes a command on the input given
  • echo shows command instead of executing
  • kill -SIGCONT sends signal to continue the process

Output is something like this:

kill -SIGCONT 214191 334813

Either remove echo, cut & paste or pipe the output through bash to actually execute it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .