168

I accidentally "stopped" my telnet process. Now I can neither "switch back" into it, nor can I kill it (it won't respond to kill 92929, where 92929 is the processid.)

So, my question is, if you have a stopped process on linux command line, how do you switch back into it, or kill it, without having to resort to kill -9?

tshepang
  • 65,642
bobobobo
  • 1,805

3 Answers3

188

The easiest way is to run fg to bring it to the foreground:

$ help fg
fg: fg [job_spec]
    Move job to the foreground.

    Place the job identified by JOB_SPEC in the foreground, making it the
    current job.  If JOB_SPEC is not present, the shell's notion of the
    current job is used.

    Exit Status:
    Status of command placed in foreground, or failure if an error occurs.

Alternatively, you can run bg to have it continue in the background:

$ help bg
bg: bg [job_spec ...]
    Move jobs to the background.

    Place the jobs identified by each JOB_SPEC in the background, as if they
    had been started with `&'.  If JOB_SPEC is not present, the shell's notion
    of the current job is used.

    Exit Status:
    Returns success unless job control is not enabled or an error occurs.

If you have just hit Ctrl Z, then to bring the job back just run fg with no arguments.

Likeyn
  • 75
terdon
  • 242,166
  • Thanks!! I suppose this is the eq of Alt+Tab. Do you know what happened as soon as I did fg telnet though. It said Terminated, presumably b/c of my previous kill cmd. – bobobobo Jan 15 '14 at 23:00
  • @bobobobo presumably, yes. Anyway, the fg does not need any arguments. If you have just hit ^Z, run fg in the same terminal and it will bring back the 1st job. – terdon Jan 15 '14 at 23:03
  • What if I want to end the process? – Aaron Franke Oct 18 '23 at 03:12
  • @AaronFranke you fg it and then Ctrl+C to kill it. – terdon Oct 18 '23 at 10:04
  • What if I am running a script with multiple commands which Ctrl+C only terminates one of the commands at a time and I want to kill the entire script? – Aaron Franke Oct 19 '23 at 02:53
  • @AaronFranke please ask a new question, show an example of the code, and the expected behavior on your chosen operating system. Normally, Ctrl+C kills the command, the thing you had launched from the command line. This would be the script, not the commands in the script, so the Ctrl+C would kill everything. – terdon Oct 19 '23 at 11:55
82

You can use jobs to list the suspended process. Take the example. Start with a process:

$ sleep 3000  

Then you suspend the process:

^Z
[1]+  Stopped                 sleep 3000

You can list the process:

$ jobs
[1]+  Stopped                 sleep 3000

and bring it back to the foreground:

$ fg %1
sleep 3000

The %1 corresponds to the [1] listed with the jobs command.

Likeyn
  • 75
Luis
  • 2,518
35

You should be able to re-start a suspended process by using the kill command to send the process the CONTINUE signal, from the command-line, thus:

kill -CONT 92929
bahamat
  • 39,666
  • 4
  • 75
  • 104
Geeb
  • 2,131
  • 2
  • 16
  • 17
  • 4
    This will cause it to resume operation, but not to be brought to the foreground. – bahamat Jan 17 '14 at 16:50
  • 1
    @bahamat Yes, quite true. One would still need to fg inside the original terminal. I like to -STOP and -CONT gui programs to save resources, but they are effectively running in the background anyway. – Geeb Jan 17 '14 at 17:10
  • In case of sleep it will kill the process which is not what you want..;) – Timo Mar 14 '18 at 16:46
  • Maybe not the correct answer, but a very useful one ! I have a script that opens several terminals in order to execute several commands concurrently. When I press ctrl-z on a terminal where a shell was launched to execute a command given as a parameter, it will stop the command. jobs then shows an empty list. I don't understand why the list is empty, but this kill -CONT is the only solution I know in this case to resume the stopped process. – SR_ May 26 '21 at 07:48