Running Linux Commands in Foreground And Background

If you have a long-term task, it is not always advisable to wait for it to finish. I mean, why keep the terminal busy for a particular command? In Linux, you send a command or process in the background for the command to execute, but the terminal will be free to execute other commands.

Directly Start a Linux process in the background 

If you know the Background Linux command or process will take a long time, it would be a better idea to start the command in the background.

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

your_command &

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

sleep 30 &

When the command ends in the background, you should see information about it in the terminal.

[1]+  Done                    sleep 30

Send a running Linux process to the background.

If you have already run a program and then realized that you should have run it in the background, do not worry. You can also send a running process in the background.

What you should do here is to use Ctrl + Z to suspend the running process and then use ‘bg‘ (background abbreviation) 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.

sameer@t4tech:~$ sleep 30
^Z
[1]+  Stopped                 sleep 30
sameer@t4tech:~$ bg
[1]+ sleep 30 &

See all processes running in the background.

Now that you know how to send the processes in the background, you may want to know what commands are running in the background.

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

jobs

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

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

Do you see numbers 1, 2, and 3, etc.? These are the job identifiers. You would also see the sign – and + in two of the commands. The + sign indicates the last job you executed or placed in the foreground. The sign – indicates the second last job you executed

Bring a process to the forefront in Linux.

Good! Then you learned to execute commands in the background on Linux. But what about bringing a process running in the background?

To send the command to the background, he used “bg”. To recover the background process, use the “fg” command.

fg

Now, if you use fg, it will bring to the first process the last process in the background job queue. In our previous example, running “fg” will bring the Vim editor back to the terminal.

If you bring a certain process to the foreground, you must specify its id of work. The job ID is the number you see at the beginning of each line in the output of the “jobs” command.

fg n

“n” is the job displayed, as shown in the output of the command jobs.

Running Processes

Open the terminal application and show all processes running in the system, including those running in the background: you can enter this command in the terminal.

ps

Killing a Process

To end the background process, use the kill command followed by the process ID:

kill processID

This was a quick one but enough for you to learn a few things about running commands in the background in Linux. If you have any questions or suggestions, leave a comment below.

>>

Recent Articles

Cybercrime forensic Paid Course Free

What is Cybercrime forensic  Cybercrime Forensic computing is the application of investigation and analysis techniques to collect and preserve evidence of a particular computing device...

Android Keylogger Hidden Keyboard

However, if Android Keylogger falls into the hands of hackers, it can be dangerous. Critical information, such as usernames and passwords, is often entered...

Kali Linux 2019.4 Released New Features With New DE, Undercover Mode And More

What’s new in Latest Kali Linux 2019.4New Desktop Environment New GTK3 Theme Kali Undercover Kali Documentation Updates BTRFS during setup PowerShell NetHunter KexNew...

How To Install Latest Kali Linux 2019.4

What is Kali Linux? Kali Linux is a Debian-based Linux operating system for hacking, Advanced Penetration Testing, Security Auditing Security research, Computer Forensics, and Reverse...

Linux Terminal Shortcuts Keys Every Power Linux User

Shortcuts help you be more productive and efficient with any tool you use. Just think about it. If someone keeps the mouse down to...

1 Comment

  1. Heya i am for the first time here. I found this board and I find
    It really helpful & it helped me out much.
    I’m hoping to offer one thing back and help others such as you aided me.

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox