


How to smoothly switch between different applications under Linux system
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:
1. Linux starts a program to execute in the background
1. Use 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.
2. Use 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# 使程序在你退出终端时仍然运行
3. Use 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
3.1 Create session
$ screen -S session_name your_program
3.2 To reconnect to this session:
$ screen -r session_name
3.3 Exit the session midway, but the program continues to run:
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. $
3.4 Ending a 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] $
3.5 Create multiple windows:
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.
3.6 Naming window:
You can name the window for easy identification. Just press Ctrl
A
and then A
(rename). Enter the new name and press Enter.
3.7 View all windows,
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才能退出该会话
二、ctrl+Z停止了进程之后,怎么再进入该程序?
在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)运行程序,那么你可能需要使用不同的方法来控制程序的运行。
三、综合example示范
$ ./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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.
