Bug 21695 - Unix jmeter start script assumes it is on PATH, not a link
Summary: Unix jmeter start script assumes it is on PATH, not a link
Status: RESOLVED FIXED
Alias: None
Product: JMeter - Now in Github
Classification: Unclassified
Component: Main (show other bugs)
Version: 1.8.1
Hardware: Other other
: P3 enhancement (vote)
Target Milestone: ---
Assignee: JMeter issues mailing list
URL:
Keywords:
: 55994 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-07-17 18:16 UTC by Frank Griswold
Modified: 2014-01-16 15:34 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Frank Griswold 2003-07-17 18:16:48 UTC
If I don't want to alter my PATH envariable, then it makes sense to symbolic
link the jmeter startup script to someplace already in my PATH. But `dirname $0`
finds the symbol's location.  Here is a fixed script (tested linux, solaris)

#! /bin/sh
#java -Xincgc -jar `dirname $0`/ApacheJMeter.jar "$@"
truedirname() {
  if test -L $0 ; then 
    x=`/bin/ls -l $0`
    for i in $x ; do
      :
    done
    case "$i" in
     /*) x=`dirname $i` ;;
     *) x=`dirname $0`/`dirname $i` ;;
    esac
  else
    msg="$msgd"
    x=`dirname $0`
  fi
  here=`pwd`
  cd $x 2>&1 > /dev/null
  pwd
  cd $here 2>&1 > /dev/null
}

java -Xincgc -jar `truedirname $0`/ApacheJMeter.jar "$@"

I don't claim this is elegant... but it works for me.
Comment 1 Mike Deeks 2007-09-27 16:09:09 UTC
(In reply to comment #0)
> If I don't want to alter my PATH envariable, then it makes sense to symbolic
> link the jmeter startup script to someplace already in my PATH. But `dirname $0`
> finds the symbol's location.  Here is a fixed script (tested linux, solaris)
> 
> #! /bin/sh
> #java -Xincgc -jar `dirname $0`/ApacheJMeter.jar "$@"
> truedirname() {
>   if test -L $0 ; then 
>     x=`/bin/ls -l $0`
>     for i in $x ; do
>       :
>     done
>     case "$i" in
>      /*) x=`dirname $i` ;;
>      *) x=`dirname $0`/`dirname $i` ;;
>     esac
>   else
>     msg="$msgd"
>     x=`dirname $0`
>   fi
>   here=`pwd`
>   cd $x 2>&1 > /dev/null
>   pwd
>   cd $here 2>&1 > /dev/null
> }
> 
> java -Xincgc -jar `truedirname $0`/ApacheJMeter.jar "$@"
> 
> I don't claim this is elegant... but it works for me.

A better way to do this is to use readlink like so:

java -Xincgc -jar $(dirname $(readlink $0)) "$@"

Readlink was originally a BSD utility, but I believe it exists in most, if not
all, Linux distos. I haven't found one yet that doesn't have it... even some of
our older RHEL3 servers have it.

P.S. the backticks ` are deprecated in Bash now. The $( ) syntax is preferred
instead. 
Comment 2 Sebb 2014-01-14 17:49:48 UTC
*** Bug 55994 has been marked as a duplicate of this bug. ***
Comment 3 Sebb 2014-01-15 00:50:50 UTC
readlink works OK for absolute soft-links and relative soft-links in the current directory.

However if a relative soft-link is accessed via the PATH, then the technique does not work; it will be necessary to convert the relative link to an absolute one.

Looks like readlink -f does what is wanted; however does it exist on all Unix-like systems?

If it does, then

java $JVM_ARGS $JMETER_OPTS -jar `dirname $0`/ApacheJMeter.jar "$@"

can simply be changed to

java $JVM_ARGS $JMETER_OPTS -jar `dirname readlink -f $0`/ApacheJMeter.jar "$@"

To allow for spaces in path names, one would need:

java $JVM_ARGS $JMETER_OPTS -jar "`dirname readlink -f $0`/ApacheJMeter.jar" "$@"

Is "readlink -f" supported on MacOS?

Seems to work OK on FreeBSD and Ubuntu.
Comment 4 Rainer Jung 2014-01-16 12:55:55 UTC
readlink is not available on Solaris.

Here's a snippet from Tomcat bin/catalina.sh that is there since more than 7 years and seems to work reliably on many platforms (as we expect Tomcat to be used on almost any platform):

...

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

# Get standard environment variables
PRGDIR=`dirname "$PRG"`

...

Regards,

Rainer
Comment 5 Sebb 2014-01-16 14:57:42 UTC
(In reply to Rainer Jung from comment #4)
> readlink is not available on Solaris.

Useful to know.

> Here's a snippet from Tomcat bin/catalina.sh that is there since more than 7
> years and seems to work reliably on many platforms (as we expect Tomcat to
> be used on almost any platform):

Excellent.
Comment 6 Sebb 2014-01-16 15:34:03 UTC
Fixed:

URL: http://svn.apache.org/r1558834
Log:
 Unix jmeter start script assumes it is on PATH, not a link
Bugzilla Id: 21695

Modified:
    jmeter/trunk/bin/jmeter.sh
    jmeter/trunk/xdocs/changes.xml


Note that an additional change to the script was needed to allow for spaces in the JMeter installation path (perhaps should have been raised as a separate bug)
Comment 7 The ASF infrastructure team 2022-09-24 20:37:30 UTC
This issue has been migrated to GitHub: https://github.com/apache/jmeter/issues/1173