这几天在linux下安装了一个websphere,需要将他设定为自启动的,到网上查询了一下资料,现在把一些心得整理下来:
首先要制作Linux下自启动文件,需要了解一下linux的启动机制:linux的六级启动,这个详细的可以参看我前面转来的一篇帖子。
了解了Linux的启动机制以后,我们了解了要做这个东西,只需要在/etc/init.d/下面添加一个启动脚本,在此脚本里设置好相关的环境即可,我们在此目录下添加一个websphereinit文件:
#!/bin/bash
#
# name: /etc/rc.d/init.d/WebShpereinit
# chkconfig: 345 99 91
# description: starts and stops WebShpere 6
start() {
#Start Websphere 6
echo "Starting Websphere......"
/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/bin/startServer.sh server1
sleep 10
echo "Started Successfully"
}
stop(){
#Stop Websphere 6
echo "Stopping Websphere..."
/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/bin/stopServer.sh server1 -username admin -password admin
}
case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo "Usage: Websphere6[start|stop]"
;;
esac
关于这个文件,我详细解释一下,首先前4行,按照这个格式写,包括空格,这里除了第三行# chkconfig: 345 99 91,需要解释一下,其他的都应该可以看懂,第三行在这里的作用是对于chkconfig的一个配置(chkconfig命令要了解一下,它有--add,--del,--list,--level等参数),[345]表示我们要写得这个服务是3,4,5级启动,[99 90]的意思是第99个开启服务,第90个关闭服务(每一级的服务都是以0-99的顺序来执行的).
下面定义了2个相当于程序里的函数start(),stop(), 然后在后面开始调用这2个函数,sleep 10 这里的作用是让Websphere启动在后台后,把cpu让出来.
case "$1" in 表示 参数第一位是
start) 表示 start 字符串
stop) 表示 stop 字符串
*) 表示 其他情况
;; 表示 调用结束
esac 表示 整个文件结束
所以,上述这段代码的意思就是,如果第一个参数为start,调用start(),如果第一个参数参数为stop,调用stop(),如果第一个参数既不是start,也是stop,那就打印出Usage: Websphere6[start|stop].
完成后(注意一点,最好用vi直接写这个文件,我原来是在window里用记事本写好,然后丢到linux里,结果有不知名的错误,应该是文件格式不对),如果该文件在/etc/init.d/下,可以直接打./websphereinit start来调试,如果调试无误,我们可以用chkconfig --add websphereinit命令来把它添加到启动服务里(这个过程执行完成后,就相当于在系统启动时,会自动在websphereinit后面加上start参数,在系统关闭时自动加上stop参数).添加完成后,可以去/etc/rc.d/rc3.d(因为我们是3级启动,所以到rc3下)下去看到S99websphereinit -> ../init.d/websphereinit这个引用.reroot服务器后,我们可以就可以在启动列表里看到websphereinit这个服务被启动了,从而我们实现了websphere的自启动.
PS:还有一些注意的,本来我是把它放在2级启动,结果在重起机器时就当在那里,后来想到,我的WebSphere里配置了全局安全认证,而且是采用database认证,database是同一台服务器的一个oracle服务,在我写得这个websphere启动里,顺序排在了oracle启动的顺序之前,所以有问题了,所以我把它改到了第3级才开始启动,问题就解决了,建议以后自己做任何的自启动服务,都将他做在最后启动. -----------------------------------------------
$附带一个jboss的自启动脚本:
#
# chkconfig: 2345 9 91
# description: start and stop JBoss services
#
. /etc/rc.d/init.d/functions
start() {
export JAVA_HOME=/usr/java/j2sdk1.4.2_11
export JBOSS_HOME=/usr/java/jboss-4.0.3SP1
export PATH=$JAVA_HOME:$PATH
#Start JBoss Server
echo "Starting JBoss"
/usr/java/jboss-4.0.3SP1/bin/run.sh -Djboss.partition.name=leila -c PlantOpsCluster &
sleep 5
echo "Started successfully!"
}
stop(){
export JAVA_HOME=/usr/java/j2sdk1.4.2_11
export JBOSS_HOME=/usr/java/jboss-4.0.3SP1
export PATH=$JAVA_HOME:$PATH
# Stop daemons.
echo "Ending JBoss"
/usr/java/jboss-4.0.3SP1/bin/shutdown.sh -S
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
;;
restart|reload)
stop
sleep 10
start
;;
condrestart)
stop
sleep 10
start
;;
*)
echo "Usage: poserver {start|stop|status|restart}"
exit 1
esac
--------------------------------------------------------
$附带一个oracle 9i的自启动脚本: (这个和前面的有些不同,有2个地方要修改.详细说明可以去网上搜一下,这个很多)
这个文件放在/etc下
#
# This file is used by ORACLE utilities. It is created by root.sh
# and updated by the Database Configuration Assistant when creating
# a database.
# A colon, ':', is used as the field terminator. A new line terminates
# the entry. Lines beginning with a pound sign, '#', are comments.
#
# Entries are of the form:
# $ORACLE_SID:$ORACLE_HOME::
#
# The first and second fields are the system identifier and home
# directory of the database respectively. The third filed indicates
# to the dbstart utility that the database should , "Y", or should not,
# "N", be brought up at system boot time.
#
# Multiple entries with the same $ORACLE_SID are not allowed.
#
#
*:/home/oracle/product/9.2.0.4.0:N
PO7:/home/oracle/product/9.2.0.4.0:Y
这个文件放在/etc/init.d下
. /etc/rc.d/init.d/functions
start() {
echo "Starting oracle:"
su - oracle -c "lsnrctl start"
su - oracle -c "/home/oracle/product/9.2.0.4.0/bin/dbstart"
}
stop() {
echo "Stopping oracle:"
su - oracle -c "lsnrctl stop"
su - oracle -c "/home/oracle/product/9.2.0.4.0/bin/dbshut"
}
case $1 in
start)
start
;;
stop)
stop
;;
*)
echo "Usage: ora9 [start|stop]"
;;
esac
这2个文件添加后,无须用chkconfig 来添加服务.