我用过Java service wrapper,很好。不过我也能自己制作脚本完成基本功能。肯定没有Java service wrapper功能全面,不过够用了,而且完全掌控。
目标:
1.从Maven项目中产生安装包
2.将安装包安装成Ubuntu service。
我的Java程序是用来发送email的,通过读取MongoDB的email内容,发送信息。同时还依赖一个config.xml文件作为启动参数,里面配置了MongoDB的基本信息和SMTP服务器的基本信息。
现在通过执行一个脚本create_deploy.sh来制作安装包。该脚本内容如下:
#!/bin/bash
source ./tool.sh
mvn clean package -DskipTests
mvn dependency:copy-dependencies
removeFolder deploy
mkdir deploy
cp -r ./target/dependency ./deploy
cp ./target/email-1.0.jar ./deploy
cp ./config.xml ./deploy
cp ./email ./deploy
cp ./install.sh ./deploy
cp ./tool.sh ./deploy
依赖的tool.sh脚本内容:
#!/bin/bash
function removeFolder {
if [ -d "$1" ]
then
echo "$2 folder exists already, remove it..."
rm -rf $1
else
echo "$2 folder doesn't exists"
fi
}
#$1 src folder
#$2 dst folder
function copyFolder {
if [ -d "$2" ]
then
echo "$2 folder exists already, remove it..."
rm -rf $2
else
echo "$2 folder doesn't exists, start copying..."
fi
cp -r $1 $2
}
#remove the folder if exists already
#$1 folder path
function createFolder {
if [ -d "$1" ]
then
echo "$1 folder exists already, remove it..."
rm -rf $1
else
echo "$1 folder doesn't exists, create it..."
fi
mkdir -p $1
}
#remove the link if exists already
#create a link($2) to file($1)
function createLink {
if [ -L "$2" ]
then
echo "$2 link exists already, removing it..."
rm $2
else
echo "$2 link doesn't exists, creating it..."
fi
echo "creating link: $2 to $1"
ln -s $1 $2
}
#$1 source file name
#$2 source folder
#$3 destion folder
#remove the file if exists already
#create a file
function copyFile {
if [ -f "$3/$1" ]
then
echo "$3/$1 exists already, removing it..."
rm $3/$1
else
echo "$3/$1 doesn't exists, copying it..."
fi
cp $2/$1 $3
}
# $1 variable name
# $2 expected value
# put this into /etc/environment if not found
function setEnv {
source /etc/environment
if [ "${!1}" = "$2" ]
then
echo "$1 is correct: $2"
else
echo "$1 is wrong: ${!1} != $2"
h=`grep "$1=\"$2\"" /etc/environment`
if [ -n "$h" ]
then
echo "/etc/environment has $1 already"
else
echo "Adding $1 into /etc/environment..."
echo "$1=\"$2\"" >> /etc/environment
fi
source /etc/environment
fi
}
#$1 means the full name of dpkg
#return 1 if dpkg is installed (found 'ii dpkg-name' in the returned string)
#otherwise return 0
function hasDpkg {
r=`dpkg -l | grep "$1"`
if [ -n "$r" ]
then
h=`dpkg -l | grep "ii $1"`
if [ -n "$h" ]
then
return 1
else
return 0
fi
else
return 0
fi
}
#$1 search string
#$2 file path
#return 1 if found
#return 0 if not found
function findStringInFile {
h=`grep "$1" $2`
echo "h: $h"
if [ -n "$h" ]
then
return 1
else
return 0
fi
}
#$1 dpkg name
function installDpkg {
hasDpkg $1
r=$?
if [ $r -eq 1 ]
then
echo "$1 was installed"
else
echo "$1 was not installed, installing..."
apt-get install $1
fi
}
#$1 user name
#return 1 if exists
#return 0 if doesn't exist
function hasUser {
h=`grep "$1" /etc/passwd`
echo "h: $h"
if [ -n "$h" ]
then
return 1
else
return 0
fi
}
#$1 user group name
#return 1 if exists
#return 0 if doesn't exist
function hasUserGroup {
h=`grep "$1" /etc/group`
echo "h: $h"
if [ -n "$h" ]
then
return 1
else
return 0
fi
}
#remove user and home folder
#then create then again
function recreateSystemUserAndFolder {
hasUser $1
r=$?
if [ $r -eq 1 ]
then
echo "$1 exits already,remove it..."
userdel -r $1
else
echo "$1 doesn't exist,create it..."
fi
adduser --home /home/$1 --system --shell /bin/bash $1
}
#remove user group
#then create it again
function recreateUserGroup {
hasUserGroup $1
r=$?
if [ $r -eq 1 ]
then
echo "$1 exists already, remove it..."
delgroup $1
else
echo "$1 doesn't exist, create it..."
fi
groupadd $1
}
create_deploy脚本创建了deploy目录,通过mvn命令生成jar包和依赖包,然后将这些都复制进deploy目录,同时还有另外三个文件:
config.xml是配置文件,启动时需要
install.sh是将email-1.0.jar安装成service的脚本
email是要放在/etc/init.d/下的脚本
创建deploy成功后,进入deploy目录,运行install.sh文件,安装成功。服务启动。
看看install.sh文件内容:
#!/bin/bash
source ./tool.sh
createFolder /home/dist/email/
cp -r ./dependency /home/dist/email/
copyFile email-1.0.jar $PWD /home/dist/email/
copyFile config.xml $PWD /home/dist/email/
copyFile email $PWD /etc/init.d
chmod +x /etc/init.d/email
update-rc.d email defaults
service email start
再看看email脚本内容:
!/bin/sh
### BEGIN INIT INFO
# Provides: email server
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: email
# Description: email server
### END INIT INFO
. /lib/lsb/init-functions
export JAVA_HOME=/usr/jdk1.6
export PATH="$JAVA_HOME/bin:$PATH"
case "$1" in
start)
log_begin_msg "Starting email server"
java -cp /home/dist/email/dependency/ -jar /home/dist/email/email-1.0.jar /home/dist/email/config.xml &
log_end_msg 0
;;
stop)
PID=`ps -ef | grep 'email-1.0.jar' | grep -v grep | awk '{print $2}'`
log_begin_msg "Stopping email server"
if [ ! -z "$PID" ]; then
kill -15 $PID
fi
log_end_msg 0
;;
restart)
$0 stop
$0 start
;;
*)
log_success_msg "Usage: service email {start|stop|restart}"
exit 1
esac
exit 0