<password.exp>
#!/usr/bin/expect
set timeout 30
if { $argc != 1 } {
send_user "/nusage: $argv0 start|stop|status/n"
exit
}
if {[lindex $argv 0] == "start"} {
set cmd "db2start"
} elseif {[lindex $argv 0] == "stop"} {
set cmd "db2stop"
} elseif {[lindex $argv 0] == "status"} {
set cmd "status"
} else {
send_user "/nusage: $argv0 start|stop|status/n"
exit
}
spawn su db2inst1
expect {
"Password:*" {
send "admin/r"
expect "*db2inst1*"
}
{db2inst1*/>} {send_user "/nIt's root path/n"}
}
#set timeout 5
#expect eof
set timeout 30
send "ps -ef/r"
expect {
{db2sysc} {
if {$cmd == "db2stop"} {
send "$cmd/r"
expect {
"*DB2STOP processing was successful*" { send_user "/ndb2 has been stopped/n"; exit 2}
"*No start database manager command was issued*" {send_user "/nNo start database manager command was issued/n"; exit 2}
timeout { send_user "/nunknown status/n"}
}
} else {
send_user "/ndb2 has been active/n"
exit 1
}
}
{db2inst1*/>} {
if {$cmd == "db2start"} {
send "db2start/r"
expect {
"*DB2START processing was successful*" { send_user "/ndb2 is starting/n"; exit 1}
"*The database manager is already active*" { send_user "/nThe database manager is already active/n"; exit 1}
timeout { send_user "/nunknown status/n"; exit 255}
}
} else {
send_user "/ndb2 has been stopped/n"
exit 2
}
}
timeout {send_user "/ncan't find db2 process, timeout./n"; exit 255}
}
exit
<autodb2start.sh>
#!/bin/bash
#DB2_HOME=/opt/ibm/db2/V9.7/bin
#User=db2inst1
# login with su db2inst1
#./password.exp status
case $1 in
start)
./password.exp start
echo "`date`: test db2 start" >> /tmp/db2start888888888888888
;;
stop)
./password.exp stop
echo "`date`: test db2 stop" >> /tmp/db2start888888888888888
;;
status)
./password.exp status
echo "`date`: test db2 status" >> /tmp/db2start888888888888888
;;
*) echo "Usage: `basename $0` (start|stop|status)"
;;
esac
copy these two files to /etc/init.d/autodb2start, and /etc/init.d/password.exp.
/sbin/chkconfig --list | grep autodb2start
autodb2start 0:off 1:off 2:off 3:off 4:off 5:off 6:off
Run command:
/sbin/chkconfig --level 5 autodb2start on
/sbin/chkconfig --list | grep autodb2start
autodb2start 0:off 1:off 2:off 3:off 4:off 5:on 6:off
Now it will run automatedly.