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

Sample code for installing ElasticSearch and Kibana under Docker

1. Introduction Elasticsearch is very popular now...

Implementation of a simple login page for WeChat applet (with source code)

Table of contents 1. Picture above 2. User does n...

TCP socket SYN queue and Accept queue difference analysis

First we must understand that a TCP socket in the...

Summary of several replication methods for MySQL master-slave replication

Asynchronous replication MySQL replication is asy...

Html comments Symbols for marking text comments in Html

HTML comments, we often need to make some HTML co...

Vue implements star rating with decimal points

This article shares the specific code of Vue to i...

Specific use of MySQL window functions

Table of contents 1. What is a window function? 1...

MySQL merges multiple rows of data based on the group_concat() function

A very useful function group_concat(), the manual...

Solve the problem of spring boot + jar packaging deployment tomcat 404 error

1. Spring boot does not support jsp jar package, ...

React's component collaborative use implementation

Table of contents Nesting Parent-child component ...

Quickly get started with VUE 3 teleport components and usage syntax

Table of contents 1. Introduction to teleport 1.1...

Solution for creating multiple databases when Docker starts PostgreSQL

1 Introduction In the article "Start Postgre...

How to use dd command in Linux without destroying the disk

Whether you're trying to salvage data from a ...