Achieve switching between different applications through the process of front and back switching.
Background: After linux starts a program, it switches to the background for execution and wants to continue operating in linux.
In Linux, you can use the following methods to start a program in the background and exit, but still keep its process running:
nohup
and &
:
$ nohup your_program &
Use the nohup
command to make the program ignore the hangup signal (SIGHUP), so that the program will continue to run even if you exit the terminal. The &
symbols cause the program to run in the background.
ctrl Z
: If you have started the program in the foreground, you can use the ctrl z
command to move it to the background:
$ ./your_program# 运行在前台 $ 按 Ctrl + Z# 将程序暂停,并将其移到后台 $ bg#在后台继续运行程序 $ disown -h# 使程序在你退出终端时仍然运行
screen
: screen
is a terminal multiplexer running on UNIX and Linux systems that allows users to launch multiple virtual terminals on one physical terminal on the same machine.
By creating a new session, you can run the program in it, and the session will remain active even if you exit the terminal. You can then reconnect to the session to view and control the program's execution.
Function of screen
Screen has three functions:
Session recovery
: As long as the Screen itself is not terminated, the session running inside it can be restored. This is especially useful for users logging in remotely - even if the network connection is interrupted, the user will not lose control of the command line session they have opened. Just log in to the host again and execute screen -r to resume the session. Also when leaving temporarily, you can also execute the detach command to suspend the Screen (switch to the background) while ensuring that the programs inside are running normally. This is very similar to VNC under the graphical interface. Multi-window
: 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 shortcut keys, and can freely redirect the input and output of each window. Session sharing
: Screen allows one or more users to log in to a session multiple times from different terminals and share all features of the session (for example, they can see the exact same output). It also provides a mechanism for window access permissions and can password-protect windows. How to usescreen
: Install sudo apt install screen
$ screen -S session_name your_program
$ screen -r session_name
If you want to exit from the screen
session without terminating the running program, you can press Ctrl
A
and then D
.
This will detach from the screen
session, but the program will still run in the background.
Example: Use secret to open multiple sessions and execute programs in the sessions.
Each session is equivalent to a logical terminal. You can exit the session and let the program still run.
$ screen -S appDemo_session ./appDemo.lua # 开启新的会话,并执行appDemo脚本程序 > help Available commands: 1. show 2. exit 3. help 4. run > # 按ctrl+AD 退出会话 [detached from 1546455.appDemo_session] $ screen -r appDemo_session [detached from 1546455.appDemo_session] $ $ $ screen -ls# 查看所有会话 There is a screen on: 1546455.appDemo_session (2024年01月24日 10时01分53秒) (Detached) 1 Socket in /run/screen/S-zsh. $
screen
session: If you want to end a screen
session and stop the programs in it, you can first reconnect to the session using screen -r [session name or ID]
and then press Ctrl
C
to terminate the program.
Next, you can use exit
or press Ctrl
D
to end the screen
session.
$ screen -S appDemo_session ./appDemo.lua -------------以下在在会话中的逻辑终端显示,退出后会消失 > help Available commands: 1. show 2. exit 3. help 4. run > exit exitCLI ------------- [screen is terminating] $
In the same screen
session, you can use Ctrl
A
and then C
to create a new window.
Each window can have its own command line history. To switch windows, just press Ctrl
A
and then N
(next) or P
(previous).
To close a window, just press Ctrl
A
then K
and select the window you want to close.
You can name the window for easy identification. Just press Ctrl
A
and then A
(rename). Enter the new name and press Enter.
You can press Ctrl a
, and then press the w
key. This will display a list of all windows in the current screen
session, including the window's number and name.
$ screen -S appDemo_session ./appDemo.lua -------------以下在在会话中的逻辑终端显示,退出后会消失 > help Available commands: 1. show 2. exit 3. help 4. run > $ ls appDemo.lua # 按ctrl+A 然后按K,输入y表示结束当前窗口 Really kill this window [y/n]
Create multiple windows in one session
$ ls appDemo.lua # 按三次ctrl+a,然后按c,创建三个各自独立的窗口,每个窗口有自己的命令行历史 # 然后按 ctrl+a,然后按w查看所有窗口 0$ appDemo.lua1$ bash2-$ bash3*$ bash # 现在相当于一个会话appDemo_session中有四个窗口,需要在四个窗口都exit才能退出该会话
在Unix和Linux系统中,当你使用Ctrl+Z
将一个程序暂停并放到后台时,该程序实际上是被挂起(暂停)了。
为了再次运行这个程序,你可以使用以下方法:
bg
命令可以将挂起的程序放到后台继续运行。bg
fg
命令。fg %1
其中%1
是你想要移到前台的挂起程序的编号。你可以使用jobs
命令查看挂起的程序的编号。
3. jobs:
使用jobs
命令可以列出当前挂起的程序。这些程序的编号可以帮助你确定要使用fg
或bg
命令时应该使用的编号。
jobs -l# 列出所有挂起的程序及其PID
kill
命令。但是,首先确保你真的想要结束它。kill %1
请注意,上述方法主要适用于shell环境中。如果你在图形界面中(如使用X Window System)运行程序,那么你可能需要使用不同的方法来控制程序的运行。
$ ./student ===== 学生信息管理系统 ===== 1. 录入学生信息 2. 显示学生信息 3. 查询学生信息 4. 修改学生信息 5. 删除学生信息 6. 退出 请选择操作: ^Z [1]+已停止 ./student $ bg [1]+ ./student & [1]+已停止 ./student $ jobs -l [1]+ 1545288 停止 (tty 输入) ./student $ ./stu11 Student Information Management System 1. Add Student 2. Display Students 3. Search Student 4. Delete Student 5. Exit Enter your choice: ^Z [2]+已停止 ./stu11 $ jobs -l [1]- 1545288 停止 (tty 输入) ./student [2]+ 1545290 停止./stu11 $
$ ./appDemo.lua > help Available commands: 1. show 2. exit 3. help 4. run > ^Z [2]+已停止 ./appDemo.lua $ jobs -l [1]- 1545480 停止./stu11 [2]+ 1545484 停止./appDemo.lua $ bg [2]+ ./appDemo.lua & $ fg %2 ./appDemo.lua Unknown command. Type 'help' for available commands. > help Available commands: 1. show 2. exit 3. help 4. run > ^Z [2]+已停止 ./appDemo.lua
The above is the detailed content of How to smoothly switch between different applications under Linux system. For more information, please follow other related articles on the PHP Chinese website!