Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementing background running Program with Linux nohup

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Does Linux nohup realize the background running program? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.

1. Background execution

Generally speaking, programs running on linux execute .sh files (. / sh files), so what if they do not affect the operation of the current CMD window and need to be run in the background?

At this point, 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

Added to the front of a command to indicate a running command that does not hang up

(2) &

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

two。 View commands running in the background

There are two commands to view, ps and jobs. The difference is that jobs can only view the tasks performed by the current terminal in the background, but it cannot be seen if the terminal is changed. The ps command is suitable for viewing the dynamics of instantaneous processes, and you can see the tasks of other terminals.

(1) jobs

[root@localhost test] # jobs [1]-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 &

There are two processes running in the background, both of which are displayed after using jobs. "+" represents the most recent task (current task) and "-" represents the previous task.

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

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

#! / bin/bashnohup 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 javaroot 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.jarroot 21662 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.jarroot 23761 0.0 112664 972 pts/0 S + 10:19 0:00 grep-- color=auto java

This is a common command to view the process, not to mention it.

A: display all programs u: display all programs in a user-based format x: display all programs, not distinguished by terminals

3. Close the programs currently running in the background

Kill command

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

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

If it is a foreground process, you can terminate it by executing Ctrl+c directly.

4. Switching and control of foreground and background processes

(1) fg command

Transfer the commands in the background to the foreground to continue to run.

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

(2) Ctrl + z command

Put a command that is being executed in the foreground in the background and in a paused state

(3) bg command

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

If you have more than one command in the background, you can first view the jobnum with jobs, and then call up the selected command with bg% jobnum to continue execution.

[root@localhost test] # jobs [1]-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 Call Task 2 to the foreground to run [root@localhost test] # fg 2nohup 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 ^ Zmax / after using ctrl+Z, put Task 2 in the background And suspend [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 Task 2 to run [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 & is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report