running db2 in bash from git's mingw on windows - windows

I have a shell script that runs a few db2 commands which I want to use on windows.
When running this in bash from msysgit 2.5.3 64bit I get an error from db2:
SQL1024N Die Verbindung zur Datenbank ging verloren. SQLSTATE=08003
for instance
start db2 with db2cmd then,
start the bash from the db2cmd window,
then run
db2 connect to <db> user <user>
db2 select * from syscat.tables
The db2 select will produce the same error.
This happens because the bash will start another subshell to execute each db2 command and the db2 connect calls another process db2bp which actually holds the connection.
When db2 connect returns the subshell is closed and the connection is lost.
This happens also when I concatenate the commands with ; or &&.
Is there a way to make bash not execute a subshell or at least not for every command?

The usual method for preventing to spawn a new shell is to prefix each command with a dot (some references for example here). You may also examine the shell built-in exec command. However, I am afraid that running a shell in Windows will have its own oddities, at least judging from my own experience, so you may want to try to experiment with different shell flavours before you get the solution right. Hope it helps anyway!

For Scripting in Bash you should add, after the connect string this little Bugger:
export DB2DBDFT=
That will ensure that all further subshells will use your db2 connection.
Hope this solves your problem.

Related

How to get ORACLE sqlplus command response time on multiple server using shell script?

I need to sqlplus command response time to check db connectivity in oracle 12c using shell script.. In order to get db connectivity time from multiple server by sqlplus command response time within 4-5 secs..
I don't know about SQLPlus, but I'd rather use TNSPING (especially as you're about to call it from operating system command prompt).

MongoDB - making db.fsyncUnlock(); work

I have a shell script that backs up MongoDB database.
I have to lock the database before backing it up.
mongo --eval "db.fsyncLock();" works fine, but when I run mongo --eval "db.fsyncUnlock();" it just waits and does nothing.
How can I make unlocking work?
edit: I know I have to keep the connection open, but how?
Executing MongoDB commands from Bash didn't really work, because you have to keep the connection open if you want to unlock the database again.
But when executing commands from Bash it connects to the database, executes the command and disconnects.
I ended up making a Javascript script and executing it from the Mongo Shell.

How to ssh into a shell and run a script and leave myself at the prompt

I am using elastic map reduce from Amazon. I am sshing into hadoop master node and executing a script like.
$EMR_BIN/elastic-mapreduce --jobflow $JOBFLOW --ssh < hivescript.sh . It sshes me into the master node and runs the hive script. The hivescript contains the following lines
hive
add jar joda-time-1.6.jar;
add jar EmrHiveUtils-1.2.jar;
and some commands to create hive tables. The script runs fine and creates the hive tables and everything else, but comes back to the prompt from where I ran the script. How do I leave it sshed into hadoop master node at the hive prompt.
Consider using Expect, then you could do something along these lines and interact at the end:
/usr/bin/expect <<EOF
spawn ssh ... YourHost
expect "password"
send "password\n"
send javastuff
interact
EOF
These are the most common answers I've seen (with the drawbacks I ran into with them):
Use expect
This is probably the most well rounded solution for most people
I cannot control whether expect is installed in my target environments
Just to try this out anyway, I put together a simple expect script to ssh to a remote machine, send a simple command, and turn control over to the user. There was a long delay before the prompt showed up, and after fiddling with it with little success I decided to move on for the time being.
Eventually I came back to this as the final solution after realizing I had violated one of the 3 virtues of a good programmer -- false impatience.
Use screen / tmux to start the shell, then inject commands from an external process.
This works ok, but if the terminal window dies it leaves a screen/tmux instance hanging around. I could certainly try to come up with a way to just re-attach to prior instances or kill them; screen (and probably tmux) can make it die instead of auto-detaching, but I didn't fiddle with it.
If using gnome-terminal, use its -x or --command flag (I'm guessing xterm and others have similar options)
I'll go into more detail on problems I had with this on #4
Make a bash script with #!/bin/bash --init-file as the shebang; this will cause your script to execute, then leave an interactive shell running afterward
This and #3 had issues with some programs that required user interaction before the shell is presented to them. Some programs (like ssh) it worked fine with, others (telnet, vxsim) presented a prompt but no text was passed along to the program; only ctrl characters like ^C.
Do something like this: xterm -e 'commands; here; exec bash'. This will cause it to create an interactive shell after your commands execute.
This is fine as long as the user doesn't attempt to interrupt with ^C before the last command executes.
Currently, the only thing I've found that gives me the behavior I need is to use cmdtool from the OpenWin project.
/usr/openwin/bin/cmdtool -I 'commands; here'
# or
/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash --norc
The resulting terminal injects the list of commands passed with -I to the program executed (no parms means default shell), so those commands show up in that shell's history.
What I don't like is that the terminal cmdtool provides feels so clunky ... but alas.

In a Linux script, is it possible to execute multiple commands in the same process?

I have a script that contains:
db2 connect to user01
db2 describe indexes for table table_desc
What I figure is happening is the process that executes the first line is different from the process that runs the second line. This means that the process that executes the first line gets the connection while the second process that runs the second line has no connection at all. This is verified because I get an error at the second line saying that there exists no database connection.
Is it possible to have the same process run both commands? Or at least a way to "join" the first process to the second?
If you want both instructions to run in the same process you need to write them to a script:
$ cat foo.db2
connect to user01
describe indexes for table table_desc
and run that script in the db2 interpreter:
db2 -f foo.db2
A Here Document might work as well:
db2 <<EOF
connect to user01
describe indexes for table table_desc
EOF
I can't test that, though, since I currently don't have a DB2 on Linux at hand.

Opening terminal windows with bash

I do a fair bit of work at the command line. When I start my computer up on of the first things I do is open up a terminal window for mysql, and one for the Rails console and usually a third running mongrel. Setting it up every morning is a bit of a drag so I would like to script it. How can I open a terminal window, log into mysql, select my development database and then leave it there at the mysql prompt waiting for me. I know how to execute a mysql statement from bash, I just don't know how to get it to leave the prompt open for me to work with after.
Hopefully that is clear!
Update:
Combining the two answers below got things working for mysql. Thanks!
Now I am trying to get a gnome-terminal window to stay open running the Rails script/server command so I can watch the output. For some reason the following closes almost immediately:
gnome-terminal -e "ruby /home/mike/projects/myapp/script/server" &
xterm provides an option for executing a command:
xterm -e myCommandToLogIntoMysql &
You can put a sequence of such xterm commands into a shell script.
How can I open a terminal window, log
into mysql, select my development
database and then leave it there at
the mysql prompt waiting for me.
mysql -u user -ppassword -D database_name
Remember not to put space between "-p" and password. Note - this is a bit insecure, as your password is visible in process list so anyone can read it using ps. You can, however, put your MySQL password in ~/.my.cnf file.

Resources