shell 脚本手动执行加载程序没问题,放在定时任务了程序未拉起

在统信UOSv20server操作系统上,手动执行的Shell脚本能成功启动etcd和java,但作为定时任务时无法正常启动。问题可能源于环境变量。解决方案是在脚本中引入`.bashrc`和`.profile`文件,确保环境变量的加载。经验证,添加这两行代码后,脚本能够正确执行etcd和java的启动命令。

今天在统信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运行时环境有关的环境变量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值