我有一个名为 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 出现时,让它保持活跃。以wait
结束脚本,等待所有 child 。
#!/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 次迭代后 sleep 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
关于linux - 无法通过 pgrep 或 ps -grep 找到在后台运行的 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58929975/