How to smoothly switch between different applications under Linux system

王林
Release: 2024-02-19 15:00:09
forward
630 people have browsed it

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 &
Copy after login

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# 使程序在你退出终端时仍然运行
Copy after login

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
Copy after login

3.2 To reconnect to this session:

$ screen -r session_name
Copy after login

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.
$
Copy after login

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]
$
Copy after login

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]
Copy after login

Create multiple windows in one session

$ ls
appDemo.lua
# 按三次ctrl+a,然后按c,创建三个各自独立的窗口,每个窗口有自己的命令行历史


# 然后按 ctrl+a,然后按w查看所有窗口
0$ appDemo.lua1$ bash2-$ bash3*$ bash

# 现在相当于一个会话appDemo_session中有四个窗口,需要在四个窗口都exit才能退出该会话
Copy after login

二、ctrl+Z停止了进程之后,怎么再进入该程序?

在Unix和Linux系统中,当你使用Ctrl+Z将一个程序暂停并放到后台时,该程序实际上是被挂起(暂停)了。

为了再次运行这个程序,你可以使用以下方法:

  • bg (Background) :使用bg命令可以将挂起的程序放到后台继续运行。
  • bg
    Copy after login
  • fg (Foreground) :如果你想要将程序重新移到前台,可以使用fg命令。
  • fg %1
    Copy after login

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

    3. jobs:

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

    jobs -l# 列出所有挂起的程序及其PID
    Copy after login
  • kill:如果你想要结束一个挂起的程序,可以使用kill命令。但是,首先确保你真的想要结束它。
  • kill %1
    Copy after login

    请注意,上述方法主要适用于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
    $
    Copy after login
    $ ./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
    Copy after login

    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!

    Related labels:
    source:mryunwei.com
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template