Linux background hangs, redirects output, temporarily stores process number, kills background process

Background hangs CMD command

CMD stands for the shell command to run in the background

Redirect the output to cmd.out, the process number is stored in pid.txt

#!/bin/sh
nohup [CMD] > cmd.out 2>&1 & echo $! > pid.txt
document Remark
pid.txt The process ID is staged
cmd.out standard output of the program

Redirect the output to nohup.out, and the process number is stored in pid.txt

#!/bin/sh
nohup [CMD] & echo $! > pid.txt

Kill the process number stored in pid.txt

kill -9 `cat pid.txt`

view stdout

tailf cmd.out

Fuzzy search process number after forgetting the process number

ps aux|grep [CMD]

.sh file

start.sh

#!/bin/bash
nohup [CMD] > cmd.out 2>&1 & echo $! > pid.txt
echo "Start Finished!"

stop.sh

#!/bin/bash
kill -9 `cat pid.txt`
echo "Stop Finished!"

Guess you like

Origin blog.csdn.net/qq_44839815/article/details/123886626
Recommended