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

What are the methods for linux to run in the background

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

Share

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

Editor to share with you what are the methods of running linux in the background. I hope you will get something after reading this article. Let's discuss it together.

1 、 nohup

Run the program by ignoring the suspend signal

Supplementary explanation

The nohup command can run the program in a way that ignores the suspend signal, and the output information of the program being run will not be displayed to the terminal.

Whether or not the output of the nohup command is redirected to the terminal, the output is appended to the nohup.out file in the current directory.

If the nohup.out file in the current directory is not writable, the output is redirected to the $HOME/nohup.out file.

If there is no file that can be created or opened for append, the command specified by the command parameter cannot be called.

If standard error is a terminal, the specified command is written to all outputs of standard error as standard output is redirected to the same file descriptor.

Simple example:

Nohup command &

Specify output instance

Nohup command > myout.file 2 > & 1 &

Other related commands

Ctrl + z # can put a command that is being executed in the foreground in the background and in a paused state.

Fg # switch background tasks to foreground execution

Bg # changes a command that is paused in the background to continue execution in the background. If you have more than one command in the background, you can use bg% jobnumber to call up the selected command

Jobs # View the status of running in the background. The jobs-l option displays the PID of all tasks.

Ps-ef | grep command or ps aux | grep command # View the process

Kill-9 process id # kill the corresponding process

More advanced uses are as follows:

Ps aux | grep command | grep-v grep | awk'{print $1}'| xargs kill-9 # indicates that the process id is obtained directly through command and kill directly

2 、 screen

Used for command line terminal handoff

A, session resumption

As long as Screen itself is not terminated, sessions running within it can be resumed. This is especially useful for users who log in remotely-even if the network connection is broken

The user also does not lose control of the command-line session that is already open. Simply log in to the host again and execute screen-r to resume the session.

Similarly, when you leave temporarily, you can also execute the detach command detach to suspend Screen (switch to background) while ensuring that the programs in it are running properly.

B, multiple windows

In the Screen environment, all sessions run independently and have their own numbers, inputs, outputs, and window caches. Users can switch between different windows through keyboard shortcuts

And you can freely redirect the input and output of each window. Screen implements basic text operations, such as copy and paste, and provides functions similar to scroll bars.

You can view a history of window conditions. Windows can also be partitioned and named, and you can monitor the activity of background windows. Session sharing Screen allows one or more users

Log in to a session multiple times from different terminals and share all the features of the session (for example, you can see exactly the same output). It also provides a mechanism for window access.

The window can be password protected.

C. Installation of screen

Yum install-y screen

Basic grammar

Screen-AmRvx-[ls-wipe] [- d] [- h] [- r] [- s] [- S]

Option

-An adjusts all windows to the size of the current terminal.

-d takes the specified screen job offline.

-h specifies the number of buffer rows for the window.

-m forces the creation of a new screen job even if it is already in the job.

-r resumes offline screen jobs.

-R first attempts to resume offline jobs. If an offline job cannot be found, a new screen job is created.

-s specifies the shell to be executed when a new window is created.

-S specifies the name of the screen job.

-v displays version information.

-x resumes previously offline screen jobs.

-ls or-- list displays all current screen jobs.

-wipe checks all current screen jobs and deletes screen jobs that are no longer available.

Common screen parameters

Screen-S yourname-> create a new session called yourname

Screen-ls-> lists all current session

Screen-r yourname-> back to the session of yourname

Screen-d yourname-> remote detach some session

Screen-d-r yourname-> end the current session and return to the session of yourname

Under each screen session, all commands start with ctrl+a (Ctrl + a).

Ctrl + a?-> displays all key binding information

Ctrl + a c-> create a new window running shell and switch to it

Ctrl + a n-> Next, switch to the next window

Ctrl + a p-> Previous, switch to the previous window

Ctrl + a 0.9-> switch to 0.9th window

Ctrl+a [Space]-> switch sequentially from Windows 0 to Windows 9

Ctrl + a Ctrl + a-> switch between two recently used window

Ctrl + a x-> locks the current window, which needs to be unlocked with a user password

Ctrl + a d-> detach, temporarily leave the current session, throw the current screen session (which may contain multiple windows) to the background for execution, and return to the state before entering the screen. At this time, in the screen session, the process (whether foreground / backend) running in each window continues to execute, even logout does not affect it.

Ctrl + a z-> put the current session in the background for execution, and you can go back with the fg command of shell.

Ctrl + a w-> displays a list of all windows

Ctrl + a t-> time, showing the current time, and the load of the system

Ctrl + a k-> kill window to forcibly close the current window

Ctrl + a-> enter copy mode. Under copy mode, you can roll back, search, and copy just like using [vi].

Cmurb Backward,PageUp

Cmurf Forward,PageDown

H (uppercase) High, move the cursor to the upper left corner

L Low, move the cursor to the lower left corner

0 move to the beginning of the line

$end of line

W forward one word, move forward in word units

B backward one word, move back in words

The first press of Space is the beginning of the marked area, and the second press is the end point.

Esc ends copy mode

Ctrl + a]-> paste, post what you just selected in copy mode

For example, please see: https://wangchujiang.com/linux-command/c/screen.html

3. Daemonize tool

# # installing git clone git://github.com/bmc/daemonize.gitsh configure & & make & & sudo make install

-a # mount to the output file instead of flushing out the default values. Only cases where-e and/or-o is specified are applied.

-e # redirects the output standard error to the specified file, replacing / dev/null

-o # redirect the output standard to the specified file, replacing / dev/null

-E name=value # adds environment variables to the daemon. The parameter type must be in name=value format. Parameters can be set multiple times.

-c directory # customize the directory before running the command.

-p pidfile # customize your own pid location.

-l lockfile # this file will be checked when a single instance starts.

-u user # in whose capacity the custom program runs.

After reading this article, I believe you have a certain understanding of "what are the methods of running linux in the background". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

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