Linux nohup Implements Background Running Programs and Viewing (nohup vs.)

  • 2021-06-28 14:39:24
  • OfStack

1. Background execution

1 Normally, running the programs on linux is to execute the.sh file (. /sh file), so what if you do not affect the operation of the current CMD window, you need to run in the background?

nohup and & Command implementation.


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 run without interruption

(2) &

Loading the last of a command means it is executed in the background

2. View commands running in the background

There are two commands to view, ps and jobs.The difference is that jobs can only view tasks performed in the background of the current terminal, and it will be invisible if the terminal is changed.The ps command, on the other hand, is useful for viewing the dynamics of an instantaneous process and seeing 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 have been started, which are shown after jobs is used. "+"Represents the most recent task (current task), "-" represents the previous task.

Use nohup and only on the current command line & The jobs command will only display it.If you write them into the.sh script and execute the script, they will not show up.

For example, after executing the following script, jobs does not show up:


#!/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 common command for viewing processes, let alone say more.

a: Show all programs u: Show x in user-centric format: Show all programs, not distinguished by terminals

3. Close programs currently running in the background

kill command

(1) View jobnum through the jobs command and execute kill%jobnum

(2) View the process number PID through the ps command, then execute kill%PID

If it is a foreground process, executing Ctrl+c directly terminates it

4. Switching and Control of Background and Front Processes

(1) fg command

Move commands in the background to the foreground to continue running

If you have more than one command in the background, you can view jobnun with jobs first, then call out the selected command with fg%jobnum.

(2) Ctrl + z command

Place a command being executed in the foreground in the background and pause

(3) bg command

Change a command that is paused in the background to continue execution in the background

If there are multiple commands in the background, you can view jobnum with jobs first, then call the selected command out with bg%jobnum to continue 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 &
 
//  Use fg  After that, the task 2 Move to 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
//  Use ctrl+Z After that, the task 2 Place in the background and pause 
[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
 
//  Use bg  After activating the task 2 Running 
[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 &

Related articles: