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

Tutorial on installing MYSQL8.X on Centos

MySQL installation (4, 5, 6 can be omitted) State...

IE6 BUG and fix is ​​a preventive strategy

Original article: Ultimate IE6 Cheatsheet: How To...

Small paging design

Let our users choose whether to move forward or ba...

Tomcat obtains the client domain name of Nginx reverse proxy

question After Nginx reverse proxy, the Tomcat ap...

Detailed explanation of React component communication

Table of contents Component Communication Introdu...

Datagrip2020 fails to download MySQL driver

If you cannot download it by clicking downloadlao...

What are the advantages of using B+ tree index in MySQL?

Before understanding this problem, let's firs...

Completely uninstall mysql. Personal test!

Cleanly uninstall MySQL. Personally tested, this ...

Detailed explanation of data sharing between Vue components

Table of contents 1. In project development, the ...

5 JavaScript Ways to Flatten Arrays

Table of contents 1. Concept of array flattening ...

Detailed explanation of how to use Vue self-nested tree components

This article shares with you how to use the Vue s...