0

I want to write a script which executes the following commands:

./virtuoso-t -f -c /home/var/lib/virtuoso/db/virtuoso.ini

which gives the following output -- after "Server online at 1111" appears on screen I want to issue another command ./isql 1111 dba dba. I dont want to issue the command "./isql 1111 dba dba" before "Server online at 1111" appears on screen -- the command ./isql 1111 dba dba should be issued in different session (screen, etc) as I want ./virtuoso-t to remain online while I execute my commands -- is there some way by which I may achieve the same

        Mon Aug 03 2015
17:08:49 { Loading plugin 1: Type `plain', file `wikiv' in `/home/jyotil/VirtuosoHugh/virtuosoInstalled/lib/virtuoso/hosting'
17:08:49   FAILED  plugin 1: Unable to locate file }
17:08:49 { Loading plugin 2: Type `plain', file `mediawiki' in `/home/jyotil/VirtuosoHugh/virtuosoInstalled/lib/virtuoso/hosting'
17:08:49   FAILED  plugin 2: Unable to locate file }
17:08:49 { Loading plugin 3: Type `plain', file `creolewiki' in `/home/jyotil/VirtuosoHugh/virtuosoInstalled/lib/virtuoso/hosting'
17:08:49   FAILED  plugin 3: Unable to locate file }
17:08:49 OpenLink Virtuoso Universal Server
17:08:49 Version 07.20.3213-pthreads for Linux as of Apr 10 2015
17:08:49 uses parts of OpenSSL, PCRE, Html Tidy
17:08:59 Database version 3126
17:09:00 SQL Optimizer enabled (max 1000 layouts)
17:09:01 Compiler unit is timed at 0.000687 msec
17:09:38 Roll forward started
17:09:38 Roll forward complete
17:09:52 Checkpoint started
17:09:54 Checkpoint finished, log reused
17:09:57 HTTP/WebDAV server online at 8890
17:09:57 Server online at 1111 (pid 4972)

For doing so I wrote the following shell script in (myScript.sh):

./virtuoso-t -f -c /home/var/lib/virtuoso/db/virtuoso.ini
./isql 1111 dba dba

However it appears that ./isql 1111 dba dba gets executed before "Server online at 1111" appears on screen. Is there some way in either python (by invoking the commands from python or c++ or using linux commands with which I may achieve the desired behaviour?

The command "./virtuoso-t -f -c /home/var/lib/virtuoso/db/virtuoso.ini" does not terminate, it remains online with the output "Server online at 1111" and in another terminal I execute queries on it using ./isql 1111 dba dba

3 Answers 3

2

You need sequential commands. To achieve this, concatenate your commands with ;, like this:

 sleep 5s ; ls

You will notice that the directory listing happens after sleep finishes.

To match your specific example, run:

./virtuoso-t -f -c /home/var/lib/virtuoso/db/virtuoso.ini ; ./isql 1111 dba dba 

To test if your server has started, adapt the following code to what you need. It uses netstat to check for opened ports on the host:

while netstat -lnt | awk '$4 ~ /:3306$/ {print "running"; system("ls"); exit 1}'; do sleep 2s; done

Note: i'm listening for a mysql server (3306 port - you need 1111)
Replace system("ls") with system("sh /full/path/to script")
print "running"; is optional (just textual status)

Sign up to request clarification or add additional context in comments.

5 Comments

or better, command1 && command2, so that the command2 won't be executed if command1 fails.
@fedorqui, good point, but as far as I know, && takes into account the returned value from the first command. And some of them also return 0 in case of success. This needs proper analysis. For a general "wait 'till first finished", i think ; does the trick
Thanks but my problem is the first command does not terminate..it remains there with the output "Server online at 1111 " and I execute "./isql 1111 dba dba" in another terminal..
I mean ./virtuoso-t starts a server..which is a database engine..it remains online with the output "Server online at 1111" on screen and using ./isql I can execute queries on this server...now I want to keep the server online in my script and issue queries using the command ./isql 1111 dba dba
@JannatArora, Using this updated answer, I was able to successfully wait for mysql to start, then run "ls" in the current directory. I think this suites your case.
2

You could reasonably wait until the line appears, by sending output to a file and grepping ...

#!/bin/bash
./virtuoso-t -f -c /home/var/lib/virtuoso/db/virtuoso.ini > log 2>&1 &
PID=$!
while [[ -d /proc/$PID && ! grep -q -m 1 "Server online at" log ]];
do
  sleep 1;
done
./isql 1111 dba dba

This:

  1. runs the server as a background process (&) and redirects its output to a file
  2. loops until the server emits the desired message to that file
  3. checks the server is alive, so a failed startup doesn't cause an infinite wait.

Edit:

Bash supports both the new [[ ... ]] and old [ ... ] conditional forms. The newer is preferred, but if your shell doesn't support it, try the old one.

It's possible your /bin/bash isn't really bash, I suppose, try running /bin/bash --version to see. If it's a simpler shell, #!/usr/bin/bash might work.

7 Comments

Thanks..what does -d /proc/$PID do.. also why are we looking in log when the output is appearing on screen
ok..do we get the process id of the background process using PID=$! .. if not then what does it so...also we are we using -d /proc/$PID
Also when I execute this I am getting " [[: not found"
Yes my shell is bash i checked using echo $0 it gave /bin/bash. But I dont know why am I getting [[: not found. I did save the script using the same first line
should i use...while // -d /proc/$PID && ! grep -q -m 1 "Server online at" log //;
|
2

I came up with this, it reads the piped output line by line until it finds the one you are looking for.

#!/bin/bash

virtuoso-t -f -c /home/var/lib/virtuoso/db/virtuoso.ini 2>&1 | while read line
do
    echo $line
    if echo $line|grep 'Server online at 1111' > /dev/null; then
        isql 1111 dba dba
    fi
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.