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 结束脚本,等待所有子进程。

#!/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/

相关文章:

linux - 定期运行的发送电子邮件应用程序的设计模式?

python - 使用python动态创建bash别名

bash - 为什么 bash 脚本中需要 "declare -f"和 "declare -a"?

php - 运行时./a.out(c编译代码)的退出状态

linux - 如果满足 shell 脚本中给定的条件,则计算平均值

java des cipher在windows上的不同结果

php - 将 Linux Curl 转换为 PHP

string - 为什么 echo "Hello World!"语句没有按预期工作?

linux - 如何连接仅在同一天生成的 linux/unix 目录中的文件?

linux - 无法使用 Linux perf,因为您的 Linux 内核不支持 PERF_COUNT_HW_REF_CPU_CYCLES 事件