search
HomeComputer TutorialsComputer KnowledgeHow 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:

How to smoothly switch between different applications under Linux system

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 (Background) :使用bg命令可以将挂起的程序放到后台继续运行。
  • bg
  • fg (Foreground) :如果你想要将程序重新移到前台,可以使用fg命令。
  • fg %1

    其中%1是你想要移到前台的挂起程序的编号。你可以使用jobs命令查看挂起的程序的编号。

    3. jobs:

    使用jobs命令可以列出当前挂起的程序。这些程序的编号可以帮助你确定要使用fgbg命令时应该使用的编号。

    jobs -l# 列出所有挂起的程序及其PID
  • kill:如果你想要结束一个挂起的程序,可以使用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!

    Statement
    This article is reproduced at:每日运维. If there is any infringement, please contact admin@php.cn delete
    Which Is the Best VPN for ChatGPT? - MiniToolWhich Is the Best VPN for ChatGPT? - MiniToolApr 29, 2025 am 12:50 AM

    If you want to use ChatGPT via VPN in an unsupported country, region, or territory, do you know which is the best VPN for ChatGPT? In this post, php.cn Software will introduce some good choices for you. You can select one according to your requiremen

    XboxPcAppFT.exe Bad Image Error: Here Is How to Fix It!XboxPcAppFT.exe Bad Image Error: Here Is How to Fix It!Apr 29, 2025 am 12:49 AM

    How to fix the “XboxPcAppFT.exe bad image” issue on Windows 11/10? This post from php.cn presents multiple methods to resolve the annoying issue. Please go on with your reading.

    How to Fix OneDrive Files Cannot Be Deleted Windows 10/11 - MiniToolHow to Fix OneDrive Files Cannot Be Deleted Windows 10/11 - MiniToolApr 29, 2025 am 12:48 AM

    What should do when you want to delete a file or folder in OneDrive, but find that OneDrive files or folders cannot be deleted? Now you can read this post from php.cn to get the best solutions to fix the “OneDrive files cannot be deleted in Windows 1

    Display Connection Might Be Limited: Key Factors & SolutionsDisplay Connection Might Be Limited: Key Factors & SolutionsApr 29, 2025 am 12:47 AM

    The error message “display connection might be limited” is an annoying issue when you start the device. In this post from php.cn, you can get detailed information about what causes this problem and how to resolve it quickly.

    Windows 11 Build 25115 Is Released to Insiders in the Dev Channel - MiniToolWindows 11 Build 25115 Is Released to Insiders in the Dev Channel - MiniToolApr 29, 2025 am 12:46 AM

    Microsoft releases a new build to Insiders in the Dev Channel and it is Windows 11 build 25115. This is a higher build compared to the build released to the Beta Channel. You can follow this php.cn post to learn some related information about it.

    How to Clean C Drive in Windows 11/10 Without Losing Data - MiniToolHow to Clean C Drive in Windows 11/10 Without Losing Data - MiniToolApr 29, 2025 am 12:45 AM

    How do I free up space on my C drive or how do I clear waste on my C drive? This is the topic that php.cn focuses on here. If your C drive is full of old apps and unnecessary programs, you can choose to clean up it. Let’s get started.

    ChatGPT 4 vs. ChatGPT 3: the Difference between Them - MiniToolChatGPT 4 vs. ChatGPT 3: the Difference between Them - MiniToolApr 29, 2025 am 12:44 AM

    ChatGPT has been updated with GPT-4. To help you better understand this update, we will introduce the differences between ChatGPT 4 and ChatGPT 3. In addition, if you want to recover deleted files on Windows, you can try php.cn Power Data Recovery.

    Media Feature Pack Windows 11 Download & Install: Power TacticsMedia Feature Pack Windows 11 Download & Install: Power TacticsApr 29, 2025 am 12:43 AM

    How can you download and install Media Feature Pack if you are using Windows 11 N or KN editions?  In this post, php.cn offers a step-by-step guide on getting Windows 11 Media Feature Pack. Let’s look through some details.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

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

    Hot Tools

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool