17

I have a legacy shell script that is being called by the Autosys job scheduler. In the script they are calling a jar file

res="`$JAVA_HOME/bin/java ....`"
echo >$res<

and I am getting the following error.

Error occurred during initialization of VM 
    java.lang.Error: Properties init: Could not determine current working directory.

So in the shell script I tried to print the current directory as shown below

echo "PWD:" "$PWD"    # Nothing gets printed.
echo "USER:" "$USER"  # User id is getting printed

if [ -d "/export/home/abc/" ]; then
    echo "Directory present"    # gets printed
    echo `ls -ltr`              # total 3 gets printed
    echo `cd /export/abc/def`
    echo `pwd`                  # nothing gets printed
fi

All the class paths are being set in the script itself and the class path looks fine. I am not getting what might be the issue here.

Also note that this script is being called by another script which intern called by Autosys job scheduler.

NJMR
  • 1,886
  • 1
  • 27
  • 46
  • 2
    echo `cd /export/abc/def` - this won't change your script's current directory – Mat Jun 03 '18 at 06:50
  • @Mat: echo `pwd` should print something. But is not printing anything. Any reason? – NJMR Jun 04 '18 at 06:53
  • Also, if I source the a script that calls another script, will they get executed in the same shell or do I need to source the inner script also? – NJMR Jun 04 '18 at 07:16
  • @NJMR, if you source and that does a nested source all will be same shell. If the nested script is run as a bash script then it will be different shell. Also if you source a shell script with a blocking command then the shell script will be blocked and further part will only be executed once the blocking executable finishes – Tarun Lalwani Jun 05 '18 at 11:20
  • *this script is being called by another script which intern called by Autosys job scheduler.* And just what does that "job" do? Does it happen to delete any directories? – Andrew Henle Jun 05 '18 at 15:28
  • @AndrewHenle: No it does not delete any directories. – NJMR Jun 05 '18 at 16:14
  • 1
    @NJMR *No it does not delete any directories.* Are you sure about that? A process has to start in a directory somewhere, and it's impossible to change the current working directory to one that doesn't exist. About the only way to get a process unable to determine its current working directory is to have the directory deleted out from under the process. – Andrew Henle Jun 05 '18 at 17:30
  • @AndrewHenle: Thanks for the hint, finally figured out that, the another process (which we were getting JAVA error) was launched by some other user and that user didn't had permissions to access the folder. Thanks for the hint. If I had an option to allocate bonty for comments, you would have got that. :-) – NJMR Jun 06 '18 at 07:34

2 Answers2

5

Thanks to Andrew for the hint.

As said in the post it was a legacy script, and had thousands of line in each of the script which made our analysis hard. But finally figured out that the process in which we were getting error was being launched by another user. That user didn't had permission to access the parent folder and hence we were getting

Could not determine current working directory.

I gave the permission on the parent folder to that user and it worked. Thanks to all of you...

NJMR
  • 1,886
  • 1
  • 27
  • 46
4

It is an expected behavior.

The script is run in a subshell, and cannot change the parent shell working directory. Its effects are lost when it finishes.

SO Reference for a workaround.

Sadiq Ali
  • 1,272
  • 2
  • 15
  • 22