Solved

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

Posted on 2004-08-27
7
439 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
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 19

Accepted Solution

by:
ramazanyich earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

823 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