0

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.

4
  • There's a * at the end of your crontab job that shouldn't be there.
    – Kusalananda
    Jul 14, 2019 at 12:02
  • Always use full paths in crontab, in this case /usr/bin/java instead of java (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.
    – Jos
    Jul 14, 2019 at 12:33
  • excuse me @Kusalananda . that was an spelling mistake! I corrected.
    – foad322
    Jul 15, 2019 at 3:47
  • @jos That's good. I redirected STDERR to log file and found that 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 with java command anymore.
    – foad322
    Jul 20, 2019 at 5:57

1 Answer 1

0

I solved that. The reason was execution of cron job by root user. By redirecting STDERR to a log file, I found that my.jar file needs an additional .cfg file to execute correctly. As you can notice that HOME=/root in cron env ,so the necessary .cfg file must be at /root directory. otherwise, you should identify your favorite path of .cfg into my.jar file. if the .cfg file is not placed in the path assigned in my.jar then exit code of java -jar my.jar|grep .... will be 0 (zero).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .