How to view and close background running programs in Linux

How to view and close background running programs in Linux

1. Run the .sh file

You can run it directly using the ./sh file, but if you want to run it in the background even if you close the current terminal, you need the nohup command and the & command.

(1) & Command

Function: Add to the end of a command to execute the command in the background

(2) nohup command

Function: Run commands without interruption

2. View the commands currently running in the background

There are two commands available, jobs and ps. The difference is that jobs is used to view the tasks running in the background of the current terminal, which cannot be seen if the terminal is changed. The ps command is used to view the dynamics of the instantaneous process and can see the background processes running in other terminals.

(1) jobs command

Function: View the tasks running in the background of the current terminal


The jobs -l option can display the PID of all tasks in the current terminal. The status of jobs can be running, stopped, or Terminated. The + sign indicates the current task, and the - sign indicates the next task.

(2) ps command

Function: View all current processes

ps -aux | grep "test.sh" #a: Display all programs u: Display in user-based format x: Display all programs, regardless of terminal

3. Close the command currently running in the background

kill command: end the process

(1) Check jobnum through the jobs command, and then execute kill %jobnum

(2) Use the ps command to view the process ID PID, and then execute kill %PID

If it is a foreground process, just execute Ctrl+c to terminate it.

4. Switching and control of foreground and background processes

(1) fg command

Function: Move the command in the background to the foreground to continue running

If there are multiple commands in the background, you can first use jobs to view jobnum, and then use fg %jobnum to call out the selected command.

(2) Ctrl + z command

Function: Put a command being executed in the foreground into the background and put it in a paused state

(3) bg command

Function: Turn a command that is paused in the background into one that continues to execute in the background

If there are multiple commands in the background, you can first use jobs to view jobnum, and then use bg %jobnum to call out the selected command for continued execution.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to install Redis in Linux, run it in the background, and set up the system to start automatically
  • Several ways of running in the background of Linux (summary)
  • Linux nohup to run programs in the background and view them (nohup and &)
  • How to start jar package and run it in the background in Linux
  • How to view and terminate running background programs in Linux
  • A brief analysis of the examples and differences of using nohup and screen to run background tasks in Linux
  • Several ways to run Python programs in the Linux background
  • How to run springboot in the background of linux
  • Linux background running node service command method
  • Solve the problem of python nohup linux background running output
  • Summary of methods for reliably running processes in the background in Linux
  • Detailed explanation of Linux background operation and shutdown, view background tasks
  • How to run Linux commands in the background

<<:  MySQL installation tutorial under Windows with pictures and text

>>:  One-click installation of MySQL 5.7 and password policy modification method

Recommend

How to open a page in an iframe

Solution: Just set the link's target attribute...

js to achieve simple front-end paging effect

Some projects have relatively simple business, bu...

Ubuntu regularly executes Python script example code

Original link: https://vien.tech/article/157 Pref...

Detailed explanation of the usage of position attribute in HTML (four types)

The four property values ​​of position are: 1.rel...

Vue large screen data display example

In order to efficiently meet requirements and avo...

Detailed explanation of prototypes and prototype chains in JavaScript

Table of contents Prototype chain diagram Essenti...

MySQL method steps to determine whether it is a subset

Table of contents 1. Problem 2. Solution Option 1...

How to use Docker plugin to remotely deploy projects to cloud servers in IDEA

1. Open port 2375 Edit docker.service vim /lib/sy...

Steps to build a Docker image using Dockerfile

Dockerfile is a text file that contains instructi...

Implementation of remote Linux development using vscode

Say goodbye to the past Before vscode had remote ...

Vue implementation example using Google Recaptcha verification

In our recent project, we need to use Google robo...

Introduction and use of js observer mode

Table of contents I. Definition 2. Usage scenario...