I can run my script as .sh file in ubuntu shell but it does not work when it is called from cron job.I run my script into an ubuntu container (using docker) as
if [ `java -jar /path/to/my.jar |grep -c 'Operation is done successfully.'` -ge 8 ]; then
echo "yes"
else echo "no"
fi
it gets correctly output of java command as a number and compares it with 8 and the rest. but when I put exactly this code in crontab, it fails. Cron job code is as follow:
# Edit this file to introduce tasks to be run by cron.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
JAVA_HOME=/usr/lib/jvm/java-7-oracle
# Each task to run has to be defined through a single line
* * * * * if [ `java -jar /path/to/my.jar |grep -c 'Operation is done successfully.'` -ge 8 ]; then echo "yes"; else echo "no";fi
the output of shell script is always "yes" however, crontab output is always "no". When I try to catch result of
`java -jar /path/to/my.jar |grep -c 'Operation is done successfully.'`
code, it returns 8 in shell but it is 0 from crontab job. I think something must be configuered in cron job to determine output of a command could be its result, not just exit code. the env
output in ubuntu container is:
GIT_PS1_SHOWDIRTYSTATE=1
GREP_COLOR=1;31
MY_DB_ENV_MYSQL_DATABASE=my_db
HOSTNAME=my-core
MY_DB_ENV_MYSQL_ROOT_PASSWORD=mypass
TERM=xterm
CLICOLOR=1
MATCHER_OPTS=[]
LS_COLORS=di=34:ln=35:so=32:pi=33:ex=1;40:bd=34;40:cd=34;40:su=0;40:sg=0;40:tw=0;40:ow=0;40:
SPRING_PROFILES_ACTIVE=production
MY_DB_PORT_3306_TCP_PORT=3306
MY_DB_PORT_3306_TCP=tcp://172.17.0.2:3306
MY_DB_ENV_MYSQL_USER=myuser
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ARTIFACT_ID=unknown
PWD=/data
JAVA_HOME=/usr/lib/jvm/java-7-oracle
AES_DB_ENV_MYSQL_PASSWORD=mypass
PS1=\[\033[40m\]\[\033[33m\][ \u@\H:\[\033[32m\]\w$(__git_ps1 " \[\033[35m\]{\[\033[36m\]%s\[\033[35m\]}")\[\033[33m\] ]$\[\033[0m\]
MY_DB_ENV_MYSQL_VERSION=5.5.42
SHLVL=1
HOME=/root
GREP_OPTIONS=--color=auto
MY_DB_NAME=/my-core/my_db
MY_DB_PORT_3306_TCP_PROTO=tcp
MY_DB_PORT_3306_TCP_ADDR=172.17.0.2
MY_DB_ENV_MYSQL_MAJOR=5.5
MY_DB_PORT=tcp://172.17.0.2:3306
_=/usr/bin/env
and env
in crontab is as
HOME=/root
LOGNAME=root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SHELL=/bin/sh
JAVA_HOME=/usr/lib/jvm/java-7-oracle
PWD=/root
can anybody tell me the reason why it happens? thanks in advance.
*
at the end of your crontab job that shouldn't be there./usr/bin/java
instead ofjava
(if that is the correct path on your system). And as long as cron jobs don't work correctly, redirect their STDOUT and STDERR to a log file.java -jar /path/to/my.jar
command has an error. I corrected that and everything goes right. by the way, using/usr/bin/java
did not differ withjava
command anymore.