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 to run a program with screen background

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

The content of this article mainly focuses on how to run the program in the background of screen. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

Use screen to run the program in the background

We often need SSH or telent to log in to the Linux server remotely and often run tasks that take a long time to complete. We can't close the window or disconnect during this period, otherwise the task will be killed and everything will be given up halfway. At this point, we can solve this problem with the screen command.

The screen command separates the current window from the task, and even if we are offline, the server is still running the task in the background. When we log back in to the server, we can read the window thread and reconnect to the task window.

One: main functions

Session recovery

As long as Screen itself is not terminated, sessions running within it can be resumed. This is particularly useful for remotely logged-in users-even if the network connection is lost, the user 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 are temporarily away, you can also execute the detach command detach to suspend Screen (switch to the background) while ensuring that the programs inside are running properly. This is very similar to VNC in a graphical interface.

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 are free to redirect the input and output of each window. Screen implements basic text operations, such as copy and paste, and provides a scrollbar-like function to view the 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 to log in to a session multiple times from different terminals and share all the features of the session (such as seeing exactly the same output). It also provides a mechanism for window access, which can protect the window with password.

GNU's Screen official site: http://www.gnu.org/software/screen/

Second: the use of screen

1, install

1) most linux systems come with screen. If it is not installed, use

Yum-y install screen

2) Grammar

$> screen [- AmRvx-ls-wipe] [- d] [- h] [- r] [- s] [- S]

-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.

2, create a new window

There are three ways to create a new window:

Screen allows you to create a new window and enter a window, but then the window has no name and you can't tell them apart.

Screen-S name creates a new window named name and merges it into the window

For example, screen-S count creates a new window called count and enters it, and you can start executing the command.

For example: screen python. / a.py create and execute a.py program

3. Session separation

After we run a program in a window, we want to log out and close the window that screen enters and do something else to let the program run in the background. At this point, you need to separate from the window session in two ways:

1) the shortcut key Ctrl a means to enter the command mode

Press the Ctrl a + d shortcut key in the current session window to detach, and the window will pop up with a [detached] prompt and return to the main window.

2) screen-d name # remotely detach a session if you have jumped out of the name window

So mom doesn't have to worry about us shutting down the terminal program anymore.

4, resume the session window

First check which windows are running

The screen-ls # list window is shown below

There are screens on:

1942.cout (Detached)

1848.name (Dead)

Remove dead screens with 'screen-wipe'.

2 Sockets in / var/run/screen/S-root.

Screen-r 1942 # enters the 1942 thread to restore the count session window or screen-r count

So we can go back to the count window.

5, kill the session window

If you want to close an extra window, there are three ways:

1) kill-9 1848 for example, 2637 technique kill-9 1848 above can kill the thread, of course, kill the window

2) use Ctrl a + k to kill the current window and the programs running in the window

3) use Ctrl an and then enter the quit command to exit the Screen session. It is important to note that this exit will kill all windows and exit all programs running in it.

6. Clear the dead window

When the window is killed, you can use screen-ls to see the word "dead" at the back of the window, indicating that the window is dead, but is still taking up space. At this point, you need to clear the window.

Screen-wipe # automatically clears dead windows

This kind of window is almost clean.

Third: other parameters and applications of screen

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

Cmura?-> displays all key binding information

Cmura c-> create a new window running shell and switch to that window

Cmura n-> Next, switch to the next window

Cmura p-> Previous, switch to the previous window

Cmura 0.9-> switch to 0.9th window

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

Cmura Cmura-> switches between two recently used window

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

Cmura d-> detach, temporarily leave the current session and change the current screen session (which may contain multiple windows)

Throw it to the background and return to the state it was before entering the screen, when the process running in each window in the screen session

Execution (both foreground / background) continues, even logout does not affect it.

Cmura z-> put the current session to be executed in the background, and you can go back with shell's fg command.

Cmura w-> displays a list of all windows

Cmura t-> time, which shows the current time and the load of the system

Cmura k-> kill window, forcibly closes the current window

Cmura [- > enters 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

Cmura]-> paste, post the content you just selected in copy mode

Screen advanced application

Session sharing

There is also a more interesting kind of session recovery, which can achieve session sharing. Suppose you and your friend log in to a machine with the same user in different locations, and then you create a screen session, and your friend can command on his terminal:

The command [root@TS-DEV] # screen-x will Attach your friend's terminal to your Screen session, and your terminal will not be Detach. In this way, you can share the same session with your friends. If you are currently in the same window, it is equivalent to sitting in front of the same monitor. Your operation will be demonstrated to your friend at the same time, and your friend's operation will be shown to you at the same time. Of course, if you switch to different windows of this session, you can do different things separately.

Session locking and unlocking

Screen allows you to use the shortcut Cmura s to lock the session. Once locked, any input screen will no longer respond. Note, however, that although there is no response on the screen, your input will be received by the process in Screen. The shortcut key Cmura Q can unlock a session.

You can also use Cmura x to lock the session, except that after locking, the session is protected by the password of the user to which Screen belongs, and you need to enter a password to continue to access the session.

Send a command to a screen session

Outside of the Screen session, you can manipulate a Screen session with the screen command, which also makes it easier to use Screen as a scripting program. With regard to the fact that the application of Screen in scripts is beyond the scope of getting started, here is just an example to experience the operation of Screen outside the session:

The command [root@TS-DEV ~] # screen-S sandy-X screen ping www.baidu.com creates a new window in a screen session called sandy and runs the ping command in it.

Thank you for your reading. I believe you have a certain understanding of "how to run programs in the background with screen". Go and practice quickly. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!

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