?
Solved

Difference between cron and shell in passing variables to a java process

Posted on 2004-08-27
7
Medium Priority
?
457 Views
Last Modified: 2013-12-15
I have a cron job that calls a shell script to monitor a process and if not running restart it BUT one of the variables is not passed if called from cron but is if the shell script is run directly.

Details:-

OS is RHEL3.

cronjob is defined for a user (user1) as:-

0,15,30,45 * * * * /home/user1/monitor.sh  

/home/user1/monitor.sh is:-

#!/bin/bash
#
#
PATH=$PATH:/usr/java/j2sdk1.4.2_04/bin:.
UNITY_OPTS1="-Dlog4j.configuration=file:ss.lcf"
UNITY_OPTS2="-DFile.encoding=UTF-8"
user=user1
process=java
if pgrep -u $user $process >/dev/null; then
check=okay
else
echo "`date` user1 down" >> /home/user1/logger
echo "`date` user1 down"
cd /home/user1/
java -cp <jar stuff> "$UNITY_OPTS1" "$UNITY_OPTS2" <org stuff> start &
echo "`date` user1 restarted" >> /home/user1/logger
fi

The problem variable is -DFile.encoding=UTF-8 defined in UNITY_OPTS2  it is NOT activated when the shell script is call from cron but YES if run
as /home/user1/monitor.sh.

Notes:
There is no difference if the -DFile.encoding=UTF-8 is put dirrectly on the java command or passed as variable as above..
<jar stuff> and <org stuff> refer to additional data that I've removed for clarity.
Everything works fine if -Dfile.encoding=UTF-8 is not used so I believe the script is okay just for that one variable there is  a problem..
0
Comment
Question by:iloop2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Expert Comment

by:gnudiff
ID: 11912199
1. try giving also UNITY_OPTS2 at the echo, like:

echo "`date` user1 restarted w/ $UNITY_OPTS2" >> /home/user1/logger line, - does it print out the correct value and prefix when run via cron or shell?

2. Forgive me for asking, but are you entirely positive that you do call the same script from cron and from shell; or perhaps you have another cron entry that calls an older version of the script at another time, somehow? It might sound strange, but these kinds of errors do happen sometimes.

0
 

Author Comment

by:iloop2
ID: 11912686
gnudiff thanks for the thoughts:

1) Yes if I do that in the logger file is "Fri Aug 27 14:13:00 BST 2004 user1 restarted w/ -DFile.encoding=UTF-8"
NB it also fails if -Dfile.encoding=UTF-8 is put directly on the java command line (was my initial method).

2) No problem asking, it is the kind of error that is very common, but I have checked multiple time and cleared the cron for the user calling the script, changed the time and the message echoed to the logger file to ensure it is the correct version. New install of OS etc so a clean setup to work with anyway.

Not sure whether it is a cron/shell or Java issue is the main problem in determining a solution, although in terms of Java it works when /home/user1/monitor.sh is called directly hence the thoughts on it being a cron/shell issue.

Thanks
0
 
LVL 4

Expert Comment

by:bobgunzel
ID: 11917075
Try printing out the environment from the script and check for differences: set >> /home/user1/logger
Java may not be able to find the necessary libraries or files for UTF-8. Since crond is started from the startup scripts, the environment parameters may not be set (correctly) for java to run properly.
You can also try to kill crond, run it again from the command prompt and see if the script works. If it does, it's definitively an environment problem.

Bob Gunzel
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 19

Accepted Solution

by:
ramazanyich earned 2000 total points
ID: 11925138
As SUN says  java parameter -Dfile.encoding is readonly. Value which you set is ignored by JVM (some versions of JDK take that parameter from command line, but you can't rely on it).
You should set environment variables in your shell script to set correct encoding which will be used by JVM.
Add following lines in the beginning of your shell script:
LC_COLLATE=en_US.utf-8; export LC_COLLATE
LC_CTYPE=en_US.utf-8; export LC_CTYPE
LC_MESSAGES=C; export LC_MESSAGES
LC_MONETARY=en_US.utf-8; export LC_MONETARY
LC_NUMERIC=en_US.utf-8; export LC_NUMERIC
LC_TIME=en_US.utf-8; export LC_TIME
0
 

Author Comment

by:iloop2
ID: 11925154
ramazanyich - thank you that looks very promising, do not have access to the server until tomorrow but will try that then.
Do you hav a URL for the info. from SUN it would be useful.
0
 

Author Comment

by:iloop2
ID: 11929281
It worked using the environment variables  you suggested ramazanyich, thanks I was under the impression that Dfile automatically set all of them but in this can from cron I guess not. Would of taken me an age to figure that out.  If you have the link to SUN detailing it would be very useful..
Thanks..
0
 
LVL 19

Expert Comment

by:ramazanyich
ID: 11929312
We had the same problem on our Solaris box. I  remember that I found page  which said that file.encoding property is readonly, but I don't have that link anymore.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Fine Tune your automatic Updates for Ubuntu / Debian
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question