【问题标题】:When launching script in background I get two processes running在后台启动脚本时,我运行了两个进程
【发布时间】:2013-12-11 14:59:44
【问题描述】:

我遇到了一个让我难过的奇怪场景。

我有一个脚本,我在后台使用 &
示例:

root@# some_script.sh &

运行后,我执行了 ps -ef | grep some_script 我看到两个进程正在运行,其中第二个进程不断获得不同的 PID,但它的父进程是我启动的进程(就像父进程正在产生死亡的子进程 - 但这从未写在代码中)。 示例:

root@# ps -ef | grep some_script.sh
root      4696 17882  0 13:30 pts/2    00:00:00 /bin/bash ./some_script.sh
root      4778  4696  0 13:30 pts/2    00:00:00 /bin/bash ./some_script.sh
root@# ps -ef | grep some_script.sh
root      4696 17882  0 13:30 pts/2    00:00:00 /bin/bash ./some_script.sh
root      4989  4696  0 13:30 pts/2    00:00:00 /bin/bash ./some_script.sh

这里给出了什么?它似乎也弄乱了脚本的输出和功能,基本上使它成为一个永无止境的过程(当我在脚本中定义了开始和停止时)。

脚本: ` #! /bin/bash

# Set Global Variables
LOGDIR="/srv/script_logs"
OUTDIR="/srv/audits"
BUCKET_LS=$OUTDIR"/LSOUT_"$i"_"$(date +%d%b%Y)".TXT"
MYCMD1="aws s3api list-objects --bucket viddler-flvs"
MYCMD2="--starting-token"
MAX_ITEMS="--max-items 10000"
MYSTARTING_TOKEN='""'
rm tokenlog.txt flv_out.txt

while [[ $MYSTARTING_TOKEN != "null" ]]
do
# First - Get the token for the next batch
CMD_PRE="$MYCMD1 $MAX_ITEMS $MYCMD2 $MYSTARTING_TOKEN"
MYSTARTING_TOKEN=($($CMD_PRE | jq -r .NextToken))
echo $MYSTARTING_TOKEN >> tokenlog.txt
# Now - get the values of the files for the existing batch
# First - re-run the batch and get the file values we want
MYOUT2=$($CMD_PRE | (jq ".Contents[] | {Key, Size, LastModified,StorageClass }"))
echo $MYOUT2 | sed 's/[{},"]//g;s/   /\n/g;s/StorageClass://g;s/LastModified://g;s/Size://g;s/Key://g;s/^ *//g;s/ *$//g' >> flv_out.txt
#echo $STARTING_TOKEN
done

`

【问题讨论】:

  • 你的脚本看起来怎么样?
  • 我需要查看 some_script.sh 中的代码来帮助解决这个问题。
  • jobs 报告什么?
  • 添加了我正在使用的脚本
  • 工作返回空白

标签: linux bash shell process background-process


【解决方案1】:

我猜你有

(
some shell instructions
)

在你的 .sh
此语法在新进程中执行命令(但命令行是相同的)。

【讨论】:

  • 那个,或者管道中的shell代码(例如somecommand | while read; do... done);这也将在子shell中运行。
  • 我看到它更像是我的命令中的管道 - 有时它是让你得到你的基础知识;)谢谢大家!
猜你喜欢
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-14
相关资源
最近更新 更多