Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How CentOS runs and controls background processes

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how CentOS runs and controls background processes". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

We often encounter the problem of logging in to a remote Linux server with ssh and running some time-consuming tasks, only to fail in the middle of the task due to the instability of the network.

This is because when the user logs out (logout) or the network is disconnected, the terminal receives a HUP (hangup) signal and shuts down all its child processes.

There are two solutions: let the process ignore the HUP signal, or let the process run in a new session and become a child process that does not belong to this terminal.

The following is an introduction to various methods of running and controlling background processes under Linux:

1.nohup

As the name implies, the purpose of nohup is to make submitted commands ignore all hangup signals.

Usage: nohup COMMAND [ARG].

2.setsid

Run the command in a new session to avoid the HUP signal sent by the current terminal.

Usage: setsid COMMAND [ARG].

3. &

You can combine () to generate a new sub-shell and put the task to run in the background in this sub-shell, so that it is not affected by the HUP signal of the current shell terminal.

Usage: (COMMAND [ARG]... &)

And my usual way of using it is:

Nohup. / filename.sh > filename.log 2 > & 1 &

Nohup. / filename.sh & > filename.log &

There are three reasons:

1) nohup ensures that the process will not be interrupted abnormally by hangup signals.

2) put the task to run in the background without occupying the current terminal

3) print the error output to log. By default, there is only standard output, but no error output.

4. Control process

We can control the commands that are put into the background with the following commands

View the background process under the current terminal:

Direct execution: jobs

Return one of the background processes found in the view to the foreground:

Direct input: fg {jobid} / / the {jobid} here is the number in the pre-process [] seen through the jobs command.

Put the process currently running in the foreground to run in the background:

First hit the shortcut key: ctrl + z / / pause the currently running process.

Execute again: bg

Terminate the process that is currently running in the foreground:

Directly hit the shortcut key: ctrl + c

5.disown

Mend, add the ability to ignore HUP signals for processes that do not use nohup and setsid.

How to use it:

Put the process currently running in the foreground to run in the background

Then execute disown-h% {jobid} / / where {jobid} is the number in the pre-process [] seen through the jobs command.

6. Realize stable background operation through screen

Screen is to establish a new full-screen virtual session terminal, which exits only when you enter exit manually. The commands executed in this session do not have to worry about the impact of HUP signals on our process, so there is no need to add "nohup" or "setsid" to each command, which is very suitable for us to carry out a large number of background tasks in a planned way. It is very convenient for us to manage these background tasks.

How to use it:

Screen / / create and enter a session immediately.

Screen-dmS {name} / / establish a session in disconnected mode and specify its session name according to our needs.

Screen-list / / lists all sessions.

Screen-r {name} / / enters the specified session exclusively.

Screen-x {name} / / enters the specified session in parallel.

Ctrl + ad / / enter the shortcut keys ctrl + an and d to temporarily exit the current session.

Exit / / after entering the specified session, execute exit to close the session.

This is the end of "how CentOS runs and controls background processes". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report