因此,对于我的班级,我的任务是编写一个脚本,一切正常,直到 ps 命令完成输出后,它还会添加一条错误消息(script.sh:第 42 行:PID 终止睡眠 900 &)第 42 行 ps 命令成功输出。
脚本.sh:
#!/bin/bash
# Clears screen and displays current running processes
clear
ps
# Pauses for 10 seconds
sleep 10
# Displays Parent Process ID using $BASHPID env var (expands to subshells)
# $BASHPID requires Bash 4 or later
echo
echo -e "Parent Process ID:\t$BASHPID"
echo
# Creates background process: Pause for 900 seconds
sleep 900 &
# Displays background jobs
jobs -l
# Pauses for 10 seconds
sleep 10
# Uses jobs -l, grep (in case other jobs are running), and cut to find PID of background job and saves it as a variable
bgjob=$(jobs -l | grep "sleep 900" | cut -d" " -f2)
# Terminates the background job created
kill -SIGTERM $bgjob
# Displays current processes so you can verify background job sucsessfully terminated
echo
ps
echo
# Pauses for 10 seconds
sleep 10
exit 0
我期望它有一个干净的输出,例如 ps 命令的输出,也许还有某种kill命令的输出,或者至少是该行上的错误
这不是脚本中的错误消息。该消息来自于您杀死了后台作业这一事实。当下一个提示出现时,您会收到消息。
举个例子,尝试:
$ sleep 50 &
$ kill $! ; sleep 5
[1]+ Terminated sleep 50
$ sleep 50 &
$ kill $!
$ #give an enter
[1]+ Terminated sleep 50