Linux nohup to run programs in the background and view them (nohup and &)

Linux nohup to run programs in the background and view them (nohup and &)

1. Background execution

Generally, programs on Linux are run by executing .sh files (./sh files). What if it does not affect the operation of the current CMD window and needs to be run in the background?

At this time, you need to use the nohup and & commands to achieve this.

nohup java -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -jar test.jar $1 $2 $3 &

(1) nohup

Add to the front of a command to indicate that the command will be run without interruption.

(2) &

Load the end of a command, indicating that this command is executed in the background

2. View the commands running in the background

There are two commands to view it, ps and jobs. The difference is that jobs can only view the tasks executed in the background of the current terminal, and it will not be visible if you change the terminal. The ps command is suitable for viewing the dynamics of instantaneous processes and can see the tasks of other terminals.

(1) jobs

[root@localhost test]# jobs
[1]- Running nohup java -Dfile.encoding=UTF-8 -Dname=Runtime-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar test.jar $1 $2 $3 & (working directory: /home/ams/ams-server/test)
[2]+ Running nohup java -Dfile.encoding=UTF-8 -Dname=Container-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar test1.jar $1 $2 $3 &

Two background processes were started one after another, and they were both displayed after using jobs. “+” represents the most recent task (current task), and “-” represents the previous task.

The jobs command can only display it when nohup and & are used in the current command line. If you write them into a .sh script and then execute the script, they will not be displayed.

For example, after executing the following script, jobs are not displayed:

#!/bin/bash
nohup java -Dfile.encoding=UTF-8 -Dname=Runtime-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar test.jar $1 $2 $3 &

(2) ps command

[root@localhost test]# ps -aux|grep java
root 21219 0.3 3.9 6258172 148900 pts/0 Sl 10:08 0:02 java -Dfile.encoding=UTF-8 -Dname=Runtime-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar test.jar
root 21662 0.2 3.0 5041008 116648 pts/0 Sl 10:10 0:01 java -Dfile.encoding=UTF-8 -Dname=Container-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar test1.jar
root 23761 0.0 0.0 112664 972 pts/0 S+ 10:19 0:00 grep --color=auto java

This is a commonly used command for viewing processes, so I won’t say more.

a: Display all programs u: Display in user-based format x: Display all programs, regardless of terminal

3. Close the current background running program

kill command

(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 controlling the foreground and background processes

(1) fg command

Bring 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

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

(3) bg command

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.

[root@localhost test]# jobs
[1]- Running nohup java -Dfile.encoding=UTF-8 -Dname=Runtime-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar test.jar $1 $2 $3 & (working directory: /home/test)
[2]+ Running nohup java -Dfile.encoding=UTF-8 -Dname=Container-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar test1.jar $1 $2 $3 &
 
// After using fg, move task 2 to the foreground [root@localhost test]# fg 2
nohup java -Dfile.encoding=UTF-8 -Dname=Container-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar test1.jar $1 $2 $3
 
^Z
// After pressing ctrl+Z, put Task 2 in the background and pause it [2]+ Stopped nohup java -Dfile.encoding=UTF-8 -Dname=Container-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar test1.jar $1 $2 $3
 
// After using bg, activate the operation of task 2 [root@localhost test]# bg 2
[2]+ nohup java -Dfile.encoding=UTF-8 -Dname=Container-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar test1.jar $1 $2 $3 &

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:
  • Solve the problem of python nohup linux background running output
  • PHP daemon process plus Linux command nohup to implement task execution once per second
  • How to use & and nohup in the background of Linux
  • Detailed explanation of the solution to the problem of nohup log output being too large under Linux
  • Linux nohup and tail-f usage
  • Nohup and background running process viewing and termination in Linux

<<:  Component design specifications for WeChat mini-program development

>>:  Some tips on speeding up the development of WeChat mini-programs

Recommend

Vue+js click arrow to switch pictures

This article example shares the specific code of ...

Detailed explanation of MySQL trigger trigger example

Table of contents What is a trigger Create a trig...

Mysql database design three paradigm examples analysis

Three Paradigms 1NF: Fields are inseparable; 2NF:...

How to extract string elements from non-fixed positions in MySQL

Preface Note: The test database version is MySQL ...

How to install mongodb 4.2 using yum on centos8

1. Make a repo file Refer to the official install...

HTML realizes hotel screening function through form

<!doctype html> <html xmlns="http:/...

Pygame code to make a snake game

Table of contents Pygame functions used Creating ...

Drawing fireworks effect of 2021 based on JS with source code download

This work uses the knowledge of front-end develop...

How to upload the jar package to nexus via the web page

When using Maven to manage projects, how to uploa...

onfocus="this.blur()" is hated by blind webmasters

When talking about the screen reading software op...

Detailed explanation of Vue development Sort component code

Table of contents <template> <ul class=&...

The connection between JavaScript constructors and prototypes

Table of contents 1. Constructors and prototypes ...

Goodbye Docker: How to Transform to Containerd in 5 Minutes

Docker is a very popular container technology. Th...