YouTube Icon

Code Playground.

How to Run Linux Commands in Background

CFG

How to Run Linux Commands in Background

Commonly when you run an order in the terminal, you need to stand by until the order completes before you can enter another. This is called running the order in the frontal area or forefront measure. At the point when a cycle runs in the forefront, it involves your shell, and you can cooperate with it utilizing the info gadgets. 

Imagine a scenario in which the order sets aside a long effort to complete, and you need to run different orders meanwhile. You have a few choices available to you. The most clear and direct choice is to begin another shell meeting and run the order in it. Another choice is to run the order out of sight. 

A foundation cycle is a cycle/order that is begun from a terminal and runs out of sight, without communication from the client. 

In this article, we will discuss the foundation measures is Linux. We will tell you the best way to begin an order out of sight and how to keep the cycle pursuing the shell meeting is shut. 

Run a Linux Command in the Background

To run an order out of sight, add the ampersand image (and) toward the finish of the order: 

command &

The shell work ID (encompassed with sections) and cycle ID will be imprinted on the terminal: 

[1] 25177

You can have various cycles running out of sight simultaneously. 

The foundation cycle will keep on composing messages to the terminal from which you conjured the order. To smother the stdout and stderr messages utilize the accompanying linguistic structure: 

command > /dev/null 2>&1 & 

>/dev/invalid 2>&1 methods divert stdout to/dev/invalid and stderr to stdout . 

Utilize the positions utility to show the status of all halted and foundation occupations in the current shell meeting: 

jobs -l

The yield incorporates the employment number, measure ID, work state, and the order that began the work: 

[1]+ 25177 Running                 ping google.com &

To bring a foundation cycle to the forefront, utilize the fg order: 

fg

 In the event that you have various foundation occupations, incorporate % and the employment ID after the order: 

fg %1

To end the foundation cycle, utilize the murder order followed by the cycle ID: 

kill -9 25177

Move a Foreground Process to Background

To move a running forefront measure out of sight: 

Stop the cycle by composing Ctrl+Z. 

Move the halted cycle to the foundation by composing bg. 

Keep Background Processes Running After a Shell Exits

In the event that your association drops or you log out of the shell meeting, the foundation measures are ended. There are a few different ways to keep the cycle pursuing the intuitive shell meeting closes. 

One route is to eliminate the employment from the shell's occupation control utilizing the repudiate shell builtin: 

disown

In the event that you have more than one foundation occupations, incorporate % and the employment ID after the order: 

disown %1

Affirm that the occupation is eliminated from the table of dynamic positions utilizing the positions - l order. To list every single running cycle, including the abandoned utilize the ps aux order. 

Another approach to keep a cycle pursuing the shell exit is to utilize nohup. 

The nohup order executes another program indicated as its contention and overlooks all SIGHUP (obstacle) signals. SIGHUP is a sign that is shipped off a cycle when its controlling terminal is shut. 

To run an order in the foundation utilizing the nohup order, type: 

nohup command &

The order yield is diverted to the nohup.out record. 

nohup: ignoring input and appending output to 'nohup.out'

In the event that you log out or close the terminal, the cycle isn't ended. 

Alternatives

There are various projects that permit you to have numerous intuitive meetings simultaneously. 

Screen 

Screen or GNU Screen is a terminal multiplexer program that permits you to begin a screen meeting and open quite a few windows (virtual terminals) inside that meeting. Cycles running in Screen will keep on running when their window isn't obvious regardless of whether you get disengaged. 

Tmux 

Tmux is an advanced option in contrast to GNU screen. With Tmux, you can likewise make a meeting and open numerous windows inside that meeting. Tmux meetings are industrious, which implies that projects running in Tmux keep on running regardless of whether you close the terminal. 

Conclusion

To run an order out of sight, incorporate and toward the finish of the order. 

At the point when you run an order out of sight, you don't need to stand by until it completes before you can execute another. 

In the event that you have any inquiries or criticism, don't hesitate to leave a remark.




CFG