我在使用maven进行打包工程时,生成execute.sh文件。
打包的zip上传到maven私服后,我在centos 7 下面下载,然后解压出execute.sh,并执行,
但是缺报出错误【bash: ./cmd.sh: /bin/sh^M: 坏的解释器: 没有那个文件或目录 解决办法】
经查,是因为MACOS WINDOWS LINUX下的换行符的原因,造成在linux下无法识别正确的换行符。
我在maven的pom文件中并没有做改动 ,而是在install.sh中加了替换【换行符】的语句。
install.sh原文: 增加了
sed -i 's/\r$//' execute.sh
#!/usr/bin/env bash
filelist=`ls -lt packages | awk '{print $9}'`
savedfile=
savedfolder=
for file in $filelist
do
if test -d "packages/$file"
then
savedfolder="packages/$file"
else
if [ "$savedfile" = "" ]
then
savedfile="packages/$file"
else [ "$savedfile" != "" ]
rm -rf "packages/$file"
fi
fi
done
echo $savedfolder
echo $savedfile
if [ "$savedfolder" != "" ]
then
rm -rf $savedfolder
fi
echo "remove original dir success"
unzip $savedfile -d "packages"
echo "unzip new package success"
rm -rf config execute.sh lib
echo "remove previous symlink success "i
filelist=`ls -lt packages | awk '{print $9}'`
folder=
for file in $filelist
do
if test -d "packages/$file"
then
folder="packages/$file"
fi
done
echo $folder
ln -s "$folder/config" config
ln -s "$folder/lib" lib
ln -s "$folder/execute.sh" execute.sh
sed -i 's/\r$//' execute.sh
chmod 755 execute.sh
echo "install success"
execute.sh文件如下:
#! /bin/sh
export profile='/etc/profile'
project_name="uav-monitor"
base_dir="home"
jar_file=
jarlist=`ls lib/"$project_name"* | awk '{print $1}'`
jar=$jarlist
jar_file="${jar#*/}"
echo "jar_file:$jar_file"
#启动方法 需要先配置环境变量JMX_ADDRESS
start(){
now=`date "+%Y-%m-%d_%H_%M_%S"`
exec nohup java -Xms128m \
-Xmx2048m \
-XX:+PrintGCDetails \
-XX:+PrintHeapAtGC \
-XX:+PrintGCDateStamps \
-XX:+PrintTenuringDistribution \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/"$base_dir"/deploy/"$project_name"/logs/dump \
-verbose:gc \
-Xloggc:/"$base_dir"/deploy/"$project_name"/logs/"$project_name"."$now".console \
-Dcom.sun.management.jmxremote=true \
-Dcom.sun.management.jmxremote.port=8022 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Djava.rmi.server.hostname=$JMX_ADDRESS \
-jar -Dconfig.base=/"$base_dir"/deploy/"$project_name"/config /"$base_dir"/deploy/"$project_name"/lib/"$jar_file" &
sleep 3
loglist=`ls -lt logs | awk '{print $9}'`
logfile=
consolefile=
extension=
for file in $loglist
do
if [ "$logfile" != "" ] && [ "$consolefile" != "" ]
then
break;
else
extension="${file##*.}"
if [ "$extension" = "log" ]
then
logfile="logs/$file"
else
consolefile="logs/$file"
fi
fi
done
pwd=`pwd`
filename="${pwd##*/}"
echo "filename: $filename"
rm -rf "$filename.log" "$filename.console"
echo "logfile: $logfile"
echo "consolefile: $consolefile"
ln -s "$logfile" "$filename.log"
ln -s "$consolefile" "$filename.console"
rm -f nohup.out
}
#停止方法
stop(){
ps -ef|grep -i "$project_name" | grep java |grep -v grep | awk '{print $2}' |while read pid
do
kill $pid
done
printf 'Stop successful\n'
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
printf 'Usage: %s {start|stop|restart}\n' "$prog"
exit 1
;;
esac