与我之前提出的问题相关这里。所以我有以下脚本:
requests=(25 50 75 100)
factors=(3 6)
graphsizes=(25 50 75)
for request in "${requests[@]}"
do
for factor in "${factors[@]}"
do
for size in "${graphsizes[@]}"
do
echo "Now Running: n = ${request}, factor = ${factor}, size = ${size}" >> nohup.out
nohup python3 -u main.py "$request" 50 "$factor" "$size" > ${request}_${factor}_${size}.log
echo "Done Running: n = ${request}, factor = ${factor}, size = ${size}" >> nohup.out
done
done
done
我的意图:我想按顺序运行所有各种参数排列main.py
;即 print Now Running...
,然后调用nohup
并运行 python 脚本,完成后 printDone running...
请注意,我无法&
在行尾添加 a nohup
,因为这会使脚本在main.py
完成之前继续运行。
但是,如果不使用&
,则在此脚本运行时我将无法再使用当前的 shell 进程。有什么办法可以解决这个问题吗?
答案1
您当然可以将整个循环放在后台。例子:
for a in 1 2; do
for b in 11 22; do
for c in 111 222; do
sleep 1; echo "$a $b $c"
done
done
done &
# notice the '&' at the end of the previous line
echo LOOP DONE
# put other commands here
wait
echo SCRIPT DONE # after 8 seconds
请注意,您永远不想使用nohup
, ;只需忽略SIGHUP
一次信号,trap '' HUP
并且“忽略”处置将由所有子进程继承。