Search code examples
linuxbashshellprocesslong-running-processes

Unable to find a shell script that runs in the background via pgrep or ps -grep


I have a file named toto.sh and the content of the file is:

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

I execute the bash script:

./toto.sh

Now i am trying to search the process like so:

pgrep -f toto.sh
ps -ef | grep toto.sh
ps aux | grep toto.sh

And i am getting no relevant results:

root     24494 15043  0 10:47 pts/5    00:00:00 grep toto.sh

However, I can see via pgrep etc. the sleep and sqlplus processes fired up through the script,

What am i doing wrong here?


Solution

  • When you want toto.sh to show up, make it stay active. End the script with wait, waiting for all children.

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

    An alternative would be adding a sleep command in the loop (I sleep 1 second after 10 iterations):

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