今天在统信UOSv20 server 版操作系统上,手动执行shell 脚本,可成功拉起etcd和java.但是当把脚本作为定时任务执行时,怎么也拉不起etcd和java.
脚本内容:
#!/bin/bash
etcd_process=`ps -ef|grep 'etcd --config' | grep -v 'grep' | wc -l`
if [ $etcd_process -lt 1 ];then
nohup /waf/tools/etcd/etcd --config-file=/waf/tools/etcd/etcd.conf > nohup.out 2>&1 &
fi
java_process=`ps aux | grep 'protection-center-core-1.0-SNAPSHOT.jar' | grep -v "grep" | wc -l`
if [ $java_process -lt 1 ];then
nohup /usr/local/jdk1.8.0_271/bin/java -jar /waf/console/web/protection-center-core-1.0-SNAPSHOT.jar --spring.profiles.active=dev --server.port=57882 > nohup.out 2>&1 &
fi
后经百度,说是环境变量的问题导致的。解决方法:
#!/bin/bash
source /root/.profile
source /etc/profile
etcd_process=`ps -ef|grep 'etcd --config' | grep -v 'grep' | wc -l`
if [ $etcd_process -lt 1 ];then
nohup /waf/tools/etcd/etcd --config-file=/waf/tools/etcd/etcd.conf > nohup.out 2>&1 &
fi
java_process=`ps aux | grep 'protection-center-core-1.0-SNAPSHOT.jar' | grep -v "grep" | wc -l`
if [ $java_process -lt 1 ];then
nohup /usr/local/jdk1.8.0_271/bin/java -jar /waf/console/web/protection-center-core-1.0-SNAPSHOT.jar --spring.profiles.active=dev --server.port=57882 > nohup.out 2>&1 &
fi
在shell脚本里增加两句指令:
source ~/.profile
source /etc/profile
profile文件内容:
root@ysq-PC:~# cat ./.profile
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
mesg n
export ETCD_UNSUPPORTED_ARCH=arm64
root@ysq-PC:~#
root@ysq-PC:~# cat /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/sbin:/usr/sbin"
fi
export PATH
if [ "$PS1" ]; then
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
tty | egrep -q tty[1-6] && export LC_ALL=C
export ETCD_UNSUPPORTED_ARCH=arm64
#environment variable for JDK
export JAVA_HOME=/usr/local/jdk1.8.0_271
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
root@ysq-PC:~#
这两个配置文件里添加过etcd和java运行时环境有关的环境变量。