linux - 无法通过 pgrep 或 ps -grep 找到在后台运行的 shell 脚本

标签 linux bash shell process long-running-processes

我有一个名为 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/

相关文章:

c - 管道 C 程序输入到 system() 函数 fuinput

python - 从文本文档中获取第 n 列(Python 3)

linux - 在bash中遍历数据文件

shell - 无法解析 GitLab 的主机名

shell - 如何在两个相同的标记模式之间获取特定数据

linux - 如何将文件内容分段打印到屏幕上?

linux - crontab 没有删除 linux 中的文件

c - 尝试写入 mtd 设备时获取 EINVAL

bash - 如何获得命令运行所需的总时间?

bash - 如何在 Bash 中定义哈希表?