2

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?

totothegreat
  • 1,633
  • 4
  • 27
  • 59

1 Answers1

3

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
Walter A
  • 19,067
  • 2
  • 23
  • 43
  • But, this way `sleep`s won't die even though `toto.sh` is killed – oguz ismail Nov 19 '19 at 09:51
  • @oguzismail Killing the porcess was not part of the question. I would remove the sleep commands `sqlplus`, they are not needed for the database. The script is an example, creating and deleting the table has no functional result. – Walter A Nov 19 '19 at 10:00
  • 1
    Oh, okay then, I didn't realize that – oguz ismail Nov 19 '19 at 10:01