您的位置:首页 > 运维架构 > Tomcat

可指定tomcat启动用户的脚本

2010-10-29 16:16 471 查看
可指定tomcat启动用户的脚本

网上很多的同仁说tomcat无法指定启动的用户,造成很大的不方便,其实自己也曾遇到过这样的问题,之前草草的写了一个脚本,不过是漏洞百出,因为需要重命名tomcat的startup.sh为tstartup.sh,虽然公司内部在使用,但是还是不好意思拿出来和大家分享。不过这里还是会和大家拿出来,以示思考的历程,实在是太简单,让同仁们见笑了!
原来的老脚本如下:
#!/bin/bash
#wrint by :liuyunfeng
#used to start tomcat use as not root
#date : 2010-09-24

runuser=www
tomdir=$(dirname "$0")

if [ "$USER" != "${runuser}" ];then
echo "Tomcat runuser is $runuser, but you are $USER."
echo "Action: We will start tomcat use ${runuser} !!"
su - $runuser <<EOF
${tomdir}/tstartup.sh
EOF
else
${tomdir}/tstartup.sh
fi
用法:重命名tomcat自带的startup.sh为tstartup.sh,然后添加该脚本为startup.sh即可。

现在的脚本是根据tomcat自身所带脚本本身上稍加改动实现的,需要在用的时候指定下用户即可。脚本中默认用户为root,可根据自己的需要修改成相应的用户。脚本如下:
#!/bin/sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
#
# $Id: startup.sh 385888 2006-03-14 21:04:40Z keith $

# Modified by : Yunfeng-Liu
# Description : Specifies the user starts tomcat
# Modified date : 2010-10-29
# -----------------------------------------------------------------------------

# Better OS/400 detection: see Bugzilla 31132
user=root # tomcat run user
os400=false
darwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
OS400*) os400=true;;
Darwin*) darwin=true;;
esac

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> /(.*/)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`/"$link"
fi
done

PRGDIR=`dirname "$PRG"`
EXECUTABLE=catalina.sh

# Check that target executable exists
if $os400; then
# -x will Only work on the os400 if the files are:
# 1. owned by the user
# 2. owned by the PRIMARY group of the user
# this will not work if the user belongs in secondary groups
eval
else
if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
echo "Cannot find $PRGDIR/$EXECUTABLE"
echo "This file is needed to run this program"
exit 1
fi
fi

###### Modified Content ###############
if [ "$USER" != "$user" ];then
exec su "$user" "$PRGDIR"/"$EXECUTABLE" start "$@"
else
exec "$PRGDIR"/"$EXECUTABLE" start "$@"
fi

用法:将原来的startup.sh删除或者是根据内容比对修改都可以,随你意!user修改成自己想要使用的用户即可,不过不要忘记给脚本赋相应的权限。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: