- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Shell script not invoked by Java which is placed i...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2015 04:28 PM
09-23-2015 04:28 PM
Shell script not invoked by Java which is placed in Crontab
Hi All,
I have a java code which invokes a shell script and is working perfectly fine when run manually.
But, when I placed this java code in crontab, only Java code is initiated by the cronjob whereas Shell script within Java is not initiated.
I provided the absolute path in all the required places,also tried 'CD' and then to execute from that path.
Crontab Entries:
* * * * * /opt/java1.5/bin/java -cp .:/home/omaduro/diksha/VOD_RT_UTIL vodRTUtilSoap
or
* * * * cd /home/omaduro/diksha/VOD_RT_UTIL && /opt/java1.5/bin/java vodRTUtilSoap
or
* * * * cd /home/omaduro/diksha/VOD_RT_UTIL; /opt/java1.5/bin/java vodRTUtilSoap
or
* * * * (cd /home/omaduro/diksha/VOD_RT_UTIL; /opt/java1.5/bin/java vodRTUtilSoap)
Inside Java to call Shell:
Process p = Runtime.getRuntime().exec(/usr/bin/sh /home/omaduro/diksha/VOD_RT_UTIL/VOD_RT_UTIL_SOAP.sh);
or
ProcessBuilder pb = new ProcessBuilder("/home/omaduro/diksha/VOD_RT_UTIL/VOD_RT_UTIL_SOAP.sh");
Process p = pb.start();
None of my logic worked, could anyone please help in calling the shell script from Java using cronjob.
Regards,
Santhosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2015 03:56 AM - edited 09-24-2015 03:58 AM
09-24-2015 03:56 AM - edited 09-24-2015 03:58 AM
Re: Shell script not invoked by Java which is placed in Crontab
cron does not login or run any profiles so it has an almost empty environment. If you run a cron job with just the set command, then compare that with set after you login, you'll see the issue difference. Java requires a lot of environment variables and as the man page for crontab states,
cron supplies a default environment for every shell, defining: HOME=user's-home-directory LOGNAME=user's-login-id PATH=/usr/bin:/usr/sbin:. SHELL=/usr/bin/sh Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.
You'll need to evaluate the environment needed by your Java code, then encapsulate the Java command with a shell script that sets these values. If you decide to use something like su - user_name to simulate a login, be sure you protect the interactive-only commands in /etc/profile and .profile by testing for an interactive session. Use the tty -s command to run interactive commands like ttytype, stty, tabs, etc only when tty -s returns zero, like this:
if tty -s then eval $(ttytype -s) # ID the terminal stty erase "^H" kill "^U" intr "^C" eof "^D" -parity ixoff stty susp \^Z dsusp \^Y
ERASE="^H" echo "$(tput rmln)$(tput sgr0)\c" tabs [[ $(id -u) -eq 0 ]] && TAG="#" || TAG="$" export PS1='$TAG ' fi
In the above code, when run as a cron or batch job (not interactive terminal connection), all the special code is skipped, This is a mandatory sysadmin task (fixing profiles) for all users to prevent the endless "not a typewriter" messages and copies of the /etc/copyright text being recorded in syslog.log and emailed when running cron, at and batch jobs. The default HP-UX /etc/profile and .profile haven't changed materially for more than 20 years and have no protection for running in batch mode.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2015 08:04 AM
09-24-2015 08:04 AM
Re: Shell script not invoked by Java which is placed in Crontab
Hi Bill,
Thanks for your inputs.
I am very new to Shell scripting and crontab.
I made an entry in crontab using "crontab -e" command and I don't use "Set" command for cron
Won't "CD" option in crontab change the directory path (provided) and execute the code from there?
Regards,
Santhosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2015 01:00 PM - edited 09-24-2015 01:04 PM
09-24-2015 01:00 PM - edited 09-24-2015 01:04 PM
Re: Shell script not invoked by Java which is placed in Crontab
Sorry, let me explain. When you login, two scripts are run to setup your environment. If you are logged in now, type the command:
set
All the lines you see are your environment. These variables such as TZ=, PATH=, PS1= and so on, are setup for you. Some of those variables are required in order for Java to run. Using cd to a directory just changes to a directory. The will not do anything about the rest of the environment. In cron the PATH variable is nothing like your login value. I suggested using set in a cron job to show you the difference. You have to determine which of those values are required in order to make your Java code work.
The other way around this (lengthy) task is to run the command using su -. I am guessing that you login as user omaduro so the cron job would be something like this:
su - omaduro -c "/opt/java1.5/bin/java /home/omaduro/diksha/VOD_RT_UTIL/vodRTUtilSoap"
This command tells cron to run su and login as user omaduro (which sets the environment) then runs the string after the -c option. I am assuming that the script you want to run is in the pathname above.
Now you post edthe crontab execution schedule as 5 stars * * * * * which means: this task will run every minute forever. I am sure this is probably not what you want. If your Java code run longer than one minute, another copy will be overlap on top of current one which may create problems. I would specify that the cron job run one time at 5 minutes in the future. If the current time is 14:22, then the scheduling numbers would be 27 14 * * *
Be sure to remove the job or it will run again tomorrow at the same time.
If this is a test, the text from the code (known as stdout) will be emailed to the omaduro user. I would suggest that all output including error messages be directed into a log file. Do this by adding:
> /var/tmp/omaduroJava.log 2>&1
at the end of the string like this:
su - omaduro -c "/opt/java1.5/bin/java /home/omaduro/diksha/VOD_RT_UTIL/vodRTUtilSoap 1>/var/tmp/omaduroJava.log 2>&1"
As every sysadmin discovers, reading the man page for each command such as cron, crontab and su will explain what is happening.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2015 01:13 AM
09-25-2015 01:13 AM
Re: Shell script not invoked by Java which is placed in crontab
> set All the lines you see are your environment.
FYI: The proper command is env(1). set will also print non-exported shell variables and you may wonder why your program can't see those variables.