Linux Background Run Task nohup Combination & Usage and How to Find Processes Accurately and kill Background Task Practice

Preface

Why nohup should be used with & I don't know if you've thought about its small size. Many other people will praise the use of screen, but I won't expand it if the actual production environment is limited.The main problem encountered this time was that crontab was used to call several different nohup background execution tasks. However, the occupancy of the same file in the code logic led to a conflict in the task process, causing the system load to be 600 high. It is not possible to blindly operate when killing tasks are needed. There are many processes that need to be killed and the process name intersects with other normal process names.pstree needs to be used appropriately to find the source of the problem.

Linux Background Run Task nohup Combination & Usage and How to Find Processes Accurately and kill Background Task Practice

Update History

November 15, 2019 - Draft

Read the original text- https://wsgzao.github.io/post...

Extended reading

Introduction to nohup

Purpose: Run commands without interruption.

Syntax: nohup Command [Arg...] [&]

  • Whether or not the output of the nohup command is redirected to the terminal, the output will be appended to the nohup.out file in the current directory.
  • If the nohup.out file in the current directory is not writable, the output is redirected to the $HOME/nohup.out file.
  • If no file can be created or opened for appending, the command specified by the Command parameter is not invoked.

Exit status: The command returns the following export values:

  • 126 The command specified by the Command parameter can be found but not invoked.
  • 127 The nohup command encountered an error or was unable to find the command specified by the Command parameter.Otherwise, the exit state of the nohup command is the exit state of the command specified by the Command parameter.

Relationship between nohup and

Run the program using nohup:

  • Output redirection, to the nohup.out file in the current directory by default
  • Use Ctrl + C to send SIGINT signal, program shutdown
  • Turn off Shell Session to send SIGHUP signal, program immunity

Use &Run the program:

  • Program goes to background
  • The result will be output to the terminal
  • Use Ctrl + C to send SIGINT signals, program immunization
  • Shut down Shell session to send SIGHUP signal, program shutdown

nohup and & use ex amp les

Typically, a combination of the two is unaffected by Ctrl C and Shell shutdown:

# The simplest background run
nohup command &
# Output default redirection to nohup.out file in current directory
nohup python main.py &  
# Custom Output File (Standard Output and Error Output merged into main.log)
nohup python main.py >> main.log 2>&1 & 
# Short form that works the same way as the previous example
nohup python main.py &> main.log &
# Do not record output information
nohup python main.py &> /dev/null &
# Does not record the output information and writes the program's process number to the pidfile.txt file to facilitate subsequent killing of the process
nohup python main.py &> /dev/null & echo $! > pidfile.txt

Since when nohup is used, the output is automatically written to the nohup.out file, and nohup.out keeps growing if the file is large, we can solve this problem with the next special file on Linux/dev/null, which is equivalent to a black hole. Anything that is output to this file will disappear, leaving only the output error message nohup command >/Dev/null 2>log &Do not use nohup command >/dev/null 2>&1 &for all information

Explain the following 2>&1 here.This involves Linux redirection, where 0, 1, and 2 are standard input, standard output, and standard error output, respectively, to specify the standard input and output that needs redirection.By default, the output is marked out, which is 1.For example, the 2>&1 we mentioned above redirects error information to standard output.

Also, if you don't want program output, there is a special file / dev/null under Linux, like a black hole, all the information output to this file will disappear. If you don't need an output log, doing so will not cause the output log file to become larger and larger and take up storage space.

Other related commands

# End current task
ctrl+c
# Place a command that is being executed in the foreground in the background and is paused
ctrl+z
# View tasks, return task numbers and process numbers
jobs -l
# Change a command that pauses in the background to continue execution in the background.If you have more than one command in the background, you can call out the selected command with BG%jobnumber.
bg %jobnumber
# Move commands in the background to the foreground to continue running.If you have more than one command in the background, you can call out the selected command with FG%jobnumber (command number, not process number)
fg %jobnumber

Write a test script

#!/bin/sh
for ((i=1; i<1000; i++))
do
    d=`date '+%Y-%m-%d %H:%M:%S'`
    echo "$d print ${i}"
    sleep 2s
done

Find Background Runner

  1. It's best to know the pid process number
  2. Filter using ps-ef or ps-aux combined with grep
  3. Confirm complex process tree structure using pstree-p
  4. Use lsof-i:80 to look up ports for process numbers
  5. Use netstat-anp | grep 80 to check port for process number, lsof is recommended

Kill Background Runner

kill

The Kill Command is used to delete an executing program or work.Kill sends the specified information to the program.The default information is SIGTERM (15), which terminates the specified program.If the program still cannot be terminated, you can use the SIGKILL (9) information to attempt to force the deletion of the program, kill-9.Program or work numbers can be viewed using ps or job instructions.

grammar
Kill (option) (parameter)

option

-a: When processing the current process, do not restrict the relationship between the command name and the process number;
-l <Information Number>: If the <Information Number>option is not added, then-l parameter lists all information names;
-p: Specifies that the kill command only prints the process number of the related process and does not send any signal;
-s <Information Name or Number>: Specify the information to send;
-u: Specify the user.

parameter
Process or Job Identification Number: Specifies the process or job to be deleted.

Example
List all signal names:

 kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

Only the ninth signal (SIGKILL) can terminate the process unconditionally. Other signal processes have the right to ignore it. Here are the common signals:

HUP 1 terminal disconnect
 INT 2 interrupt (same as Ctrl + C)
QUIT 3 quit (same as Ctrl + \)
TERM 15 Termination
 KILL 9 Forced Termination
 CONT 18 continues (fg/bg command, as opposed to STOP)
STOP 19 paused (same as Ctrl + Z)

Find the process with ps and kill it:

ps -ef | grep vim
root      3268  2884  0 16:21 pts/1    00:00:00 vim install.log
root      3370  2822  0 16:21 pts/0    00:00:00 grep vim

kill 3268
kill 3268
-bash: kill: (3268) - No process

killall

Almost all killall and pill commands use the name of a process to kill a process, which can kill a group of processes with the same name.We can use the kill command to kill the process with the specified process PID. If we want to find the process we need to kill, we also need to use ps and other commands before cooperating with grep to find the process. Kill all is a good command to combine the two processes.

grammar
Killall (option) (parameter)

option

-e: Exact matching of long names;
-l: Ignore differences in case;
-p: Kill the process group to which the process belongs;
-i: Interactively kill the process, and confirm before killing the process;
-l: Print a list of all known signals;
-q: If no process is killed.No information is output;
-r: Use a regular expression to match the name of the process to be killed;
-s: Replace the default signal "SIGTERM" with the specified process number;
-u: Kill the process of the specified user.

parameter
Process name: Specifies the name of the process to be killed.

Example
Kill all processes with the same name

killall vi

Use pstree to find and kill complex processes

Three common kill commands

  • kill
  • pkill
  • killall

Match Find Process command

  • pidof
  • pstree
# Given the process number, output the process number of the background running program at startup, then read the process number to kill the background program:
kill -9 `cat pidfile.txt`

# More and regular processes do not conflict with other normal processes
killall Process name
kill -9 $(pidof Process name)

# The number of processes is large, the law is not obvious, and the normal process is mixed up
pstree -p

# Complex scenarios like the real cases I encountered
|-crond(127436)-+-crond(138887)---bash(138892)---bash(138895)---grep(140604)
|               |-crond(139310)---bash(139323)---bash(139324)---python(139431)
|               |-crond(139311)---bash(139325)---bash(139331)---python(139452)
|               |-crond(139312)---bash(139318)---bash(139319)---python(139442)
|               |-crond(139313)---bash(139317)---bash(139320)---python(139444)
|               |-crond(139314)---bash(139329)---bash(139340)---python(139443)
|               |-crond(139315)---bash(139327)---bash(139339)---grep(140768)
|               |-crond(139651)---bash(139660)---bash(139661)---python(139915)
|               |-crond(139652)---bash(139664)---bash(139666)---python(139916)
|               |-crond(139653)---bash(139663)---bash(139665)---python(139914)
|               |-crond(139654)---bash(139675)---bash(139683)---python(139918)
|               |-crond(139655)---bash(139668)---bash(139677)---python(139913)
|               `-crond(139656)---bash(139669)---bash(139682)---grep(139780)

# If you like to use grep, you can set a few more filter conditions together.
ps -ef | grep 'python' | grep -v grep | awk '{print $2}' |xargs kill -9

Reference Article

Linux background running tasks nohup and&

3 Easy Ways to Kill or Terminate a Process on Linux

Keywords: Linux Python shell vim

Added by blmg911 on Thu, 12 Dec 2019 05:00:04 +0200