Table of Contents
2. Use ctrl Z: " >2. Use ctrl Z:
3. Use screen: " >3. Use screen:
3.1 Create session
3.2 To reconnect to this session:
3.3 Exit the session midway, but the program continues to run:
3.4 Ending a screen session:
3.5 Create multiple windows:
3.6 Naming window:
3.7 View all windows,
二、ctrl+Z停止了进程之后,怎么再进入该程序?" >二、ctrl+Z停止了进程之后,怎么再进入该程序?
三、综合example示范" >三、综合example示范
Home Computer Tutorials Computer Knowledge How to smoothly switch between different applications under Linux system

How to smoothly switch between different applications under Linux system

Feb 19, 2024 pm 03:00 PM
linux program Backstage

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 of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    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.

    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

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    PHP Tutorial
    1488
    72
    How does the cost of ownership differ between Linux and Windows? How does the cost of ownership differ between Linux and Windows? Jun 09, 2025 am 12:17 AM

    Linux's cost of ownership is usually lower than Windows. 1) Linux does not require license fees, saving a lot of costs, while Windows requires purchasing a license. 2) Linux has low hardware requirements and can extend the service life of the device. 3) The Linux community provides free support to reduce maintenance costs. 4) Linux is highly secure and reduces productivity losses. 5) The Linux learning curve is steep, but Windows is easier to use. The choice should be based on specific needs and budget.

    How does the performance of I/O operations differ between Linux and Windows? How does the performance of I/O operations differ between Linux and Windows? Jun 07, 2025 am 12:06 AM

    LinuxoftenoutperformsWindowsinI/Operformanceduetoitscustomizablekernelandfilesystems,whileWindowsoffersmoreuniformperformanceacrosshardware.1)LinuxexcelswithcustomizableI/OschedulerslikeCFQandDeadline,enhancingperformanceinhigh-throughputapplications

    How to install Linux alongside Windows (dual boot)? How to install Linux alongside Windows (dual boot)? Jun 18, 2025 am 12:19 AM

    The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.

    How to enable the EPEL (Extra Packages for Enterprise Linux) repository? How to enable the EPEL (Extra Packages for Enterprise Linux) repository? Jun 17, 2025 am 09:15 AM

    The key to enabling EPEL repository is to select the correct installation method according to the system version. First, confirm the system type and version, and use the command cat/etc/os-release to obtain information; second, enable EPEL through dnfinstallepel-release on CentOS/RockyLinux, and the 8 and 9 version commands are the same; third, you need to manually download the corresponding version of the .repo file and install it on RHEL; fourth, you can re-import the GPG key when encountering problems. Note that the old version may not be supported, and you can also consider enabling epel-next to obtain the test package. After completing the above steps, use dnfrepolist to verify that the EPEL repository is successfully added.

    How does Linux perform compared to Windows for web server workloads? How does Linux perform compared to Windows for web server workloads? Jun 08, 2025 am 12:18 AM

    Linux usually performs better in web server performance, mainly due to its advantages in kernel optimization, resource management and open source ecosystem. 1) After years of optimization of the Linux kernel, mechanisms such as epoll and kqueue make it more efficient in handling high concurrent requests. 2) Linux provides fine-grained resource management tools such as cgroups. 3) The open source community continuously optimizes Linux performance, and many high-performance web servers such as Nginx are developed on Linux. By contrast, Windows performs well when handling ASP.NET applications and provides better development tools and commercial support.

    How to choose a Linux distro for a beginner? How to choose a Linux distro for a beginner? Jun 19, 2025 am 12:09 AM

    Newbie users should first clarify their usage requirements when choosing a Linux distribution. 1. Choose Ubuntu or LinuxMint for daily use; programming and development are suitable for Manjaro or Fedora; use Lubuntu and other lightweight systems for old devices; recommend CentOSStream or Debian to learn the underlying principles. 2. Stability is preferred for UbuntuLTS or Debian; you can choose Arch or Manjaro to pursue new features. 3. In terms of community support, Ubuntu and LinuxMint are rich in resources, and Arch documents are technically oriented. 4. In terms of installation difficulty, Ubuntu and LinuxMint are relatively simple, and Arch is suitable for those with basic needs. It is recommended to try it first and then decide.

    How to add a new disk to Linux How to add a new disk to Linux Jun 27, 2025 am 12:15 AM

    The steps to add a new hard disk to the Linux system are as follows: 1. Confirm that the hard disk is recognized and use lsblk or fdisk-l to check; 2. Use fdisk or parted partitions, such as fdisk/dev/sdb and create and save; 3. Format the partition to a file system, such as mkfs.ext4/dev/sdb1; 4. Use the mount command for temporary mounts, such as mount/dev/sdb1/mnt/data; 5. Modify /etc/fstab to achieve automatic mount on the computer, and test the mount first to ensure correctness. Be sure to confirm data security before operation to avoid hardware connection problems.

    Where are system logs located in Linux? Where are system logs located in Linux? Jun 24, 2025 am 12:15 AM

    Logs in Linux systems are usually stored in the /var/log directory, which contains a variety of key log files, such as syslog or messages (record system logs), auth.log (record authentication events), kern.log (record kernel messages), dpkg.log or yum.log (record package operations), boot.log (record startup information); log content can be viewed through cat, tail-f or journalctl commands; application logs are often located in subdirectories under /var/log, such as Apache's apache2 or httpd directory, MySQL log files, etc.; at the same time, it is necessary to note that log permissions usually require s

    See all articles