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

Some common advanced SQL statements in MySQL

MySQL Advanced SQL Statements use kgc; create tab...

How to monitor global variables in WeChat applet

I recently encountered a problem at work. There i...

Tutorial on installing MySQL 8.0.11 using RPM on Linux (CentOS7)

Table of contents 1. Installation preparation 1. ...

A brief discussion of 3 new features worth noting in TypeScript 3.7

Table of contents Preface Optional Chaining Nulli...

Analysis of pitfalls in rounding operation of ROUND function in MySQL

This article uses examples to illustrate the pitf...

About the selection of time date type and string type in MySQL

Table of contents 1. Usage of DATETIME and TIMEST...

Introduction to TypeScript basic types

Table of contents 1. Basic types 2. Object Type 2...

MySQL view introduction and basic operation tutorial

Preface View is a very useful database object in ...

MySQL multi-table query detailed explanation

Time always passes surprisingly fast without us n...

Uniapp WeChat applet: Solution to key failure

uniapp code <template> <view> <ima...

Summary of the use of special operators in MySql

Preface There are 4 types of operators in MySQL, ...

12 types of component communications in Vue2

Table of contents 1. props 2..sync 3.v-model 4.re...