What command would you enter at the command prompt to start the gedit program in the background?

In the unresponsive terminal:

  1. Hit Ctrl+Z.
  2. Type bg and enter.
  3. Type disown and enter.

Long Answer

In the unresponsive terminal, hit Ctrl+Z, this will "pause" the process (or "job") and return the console control to you. However, you'll notice that gedit becomes unresponsive and you can't use it.

Extra: if you want to, you can execute the command jobs, you'll notice that it'll read Stopped for the gedit command, that's why you can't use it.

To make the job successfully run in the background (i.e. to make gedit responsive again), execute the command bg (meaning background). You'll now be able to use gedit, and at the same time have the prompt to yourself.

Extra: now, if you execute jobs, you'll notice that it'll read Running.

You can overcome all of this from the very beginning. When you're launching gedit from the terminal, add an & to the end of the command, so something like this gedit /path/to/file &. This will launch gedit in the background from the first place (you might need to hit Enter a couple of times to get the console control back).

Extra: if you were following these extra notes, you might have noticed that the second time you did jobs, you could see that bash added a & to the end of the gedit command.

Once you get used to this system, you might notice that if you close the terminal, gedit will also terminate, without even a confirmation dialog. To prevent this from happening, run disown, which will detach the gedit process from the terminal, removing it from the list returned by jobs.

If you have a long-running task, it’s not always wise to wait for it to finish. I mean why keep the terminal occupied for a particular command? In Linux, you can send a command or process to the background so that the command would be running but the terminal will be free for you to run other commands.

In this tutorial, I’ll show you a couple of ways to send a process in the background. I’ll also show you how to bring the background processes back to the foreground.

Start a Linux process in the background directly

If you know that the command or process is going to take a long time, it would be a better idea to start the command in the background itself.

To run a Linux command in the background, all you have to do is to add an ampersand (&) at the end of the command, like this:

your_command &

Let’s take a simple bash sleep command and send it to the background.

sleep 60 &

When the command finishes in the background, you should see information about that on the terminal.

[1]+ Done sleep 60

Send a running Linux process to the background

If you already ran a program and then realized that you should have run it in the background, don’t worry. You can send a running process to the background as well.

What you have to do here is to use Ctrl+Z to suspend the running process and then use ‘bg‘ (short for background) to send the process in the background. The suspended process will now run in the background.

running_command ^z bg

Let’s take the same example as before.

[email protected]:~$ sleep 60 ^Z [1]+ Stopped sleep 60 [email protected]:~$ bg [1]+ sleep 60 &

See all processes running in the background

Now that you know how to send the processes in the background, you might be interested in knowing which commands are running in the background.

For this purpose, you can enter this command in the terminal:

jobs

Let’s put some commands in the background first.

firefox & gedit & vim &

Now the jobs command will show you all the running jobs/processes/commands in the background like this:

jobs [1] Running firefox & [2]- Running gedit & [3]+ Stopped vim

Do you notice the numbers [1], [2] and [3] etc? These are the job ids. You would also notice the – and + sign on two of the commands. The + sign indicates the last job you have run or foregrounded. The – sign indicates the second last job that you ran or foregrounded.

Bring a Process to Foreground in Linux

Alright! So you learned to run commands in the background in Linux. But what about bringing a process running in the background to the foreground again?

To send the command to the background, you used ‘bg’. To bring the background process back, use the command ‘fg’.

fg

Now if you simply use fg, it will bring the last process in the background job queue to the foreground. In our previous example, running ‘fg’ will bring Vim editor back to the terminal.

If you want to bring a certain process to the foreground, you need to specify its job id. The job id is the number you see at the beginning of each line in the output of the ‘jobs’ command.

fg n

Where n is the job id as displayed in the output of the command jobs.

That’s it

This was a quick one but enough for you to learn a few things about running commands in the background in Linux. I would advise learning nohup command as well. This command lets you run commands in the background even after you log out of the session.

If you have questions or suggestions, please leave a comment below.

What command would you enter at the command prompt to start the gedit program in the background?

E179 Lecture 3 - 4 Notes 2020.doc

Pre reading ch 8 & 9.docx

South-Training-Design-District.docx

Chapter 1 Review Quiz.docx

assignment 2.edited.docx

314-report-by-TANZIL.docx

Readings & Case Review #1 - Panic in the Streets (2).docx

lab