Solved

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

Posted on 2004-08-27
7
428 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now