我有一个名为toto.sh的文件,该文件的内容为:

#!/bin/sh
for i in $(seq 1 100);
do
   echo "CREATE TABLE test_$i (id NUMBER NOT NULL);
   ! sleep 10
   select * from test_$i;
   ! sleep 10
   DROP TABLE test_$i;" | sqlplus system/mypassword &
done

我执行bash脚本:
./toto.sh

现在,我试图像这样搜索过程:
pgrep -f toto.sh
ps -ef | grep toto.sh
ps aux | grep toto.sh

而且我没有得到任何相关结果:
root     24494 15043  0 10:47 pts/5    00:00:00 grep toto.sh

但是,我可以通过pgrep等查看。通过脚本启动了sleep和sqlplus进程,

我在这里做错了什么?

最佳答案

当您希望toto.sh出现时,使其保持 Activity 状态。以wait结束脚本,等待所有子级。

#!/bin/bash
for i in $(seq 1 100);
do
   echo "CREATE TABLE test_$i (id NUMBER NOT NULL);
   ! sleep 10
   select * from test_$i;
   ! sleep 10
   DROP TABLE test_$i;" | sqlplus system/mypassword &
done
wait

另一种选择是在循环中添加一个sleep命令(我在10次迭代后睡眠1秒):
#!/bin/bash
for i in $(seq 1 100);
do
   echo "CREATE TABLE test_$i (id NUMBER NOT NULL);
   ! sleep 10
   select * from test_$i;
   ! sleep 10
   DROP TABLE test_$i;" | sqlplus system/mypassword &
   ((i%10==0)) && { echo "i=$i"; sleep 1; }
done

10-04 21:56
查看更多