0

I am running a script containing the following commands

#!/bin/bash ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

I have a java wrapper for executing this script using Runtime.exec(...). Running this java code works fine all the time.

The problem is when using running this java method under tomcat it give me an error
script.sh: line 3: ifconfig: command not found
Restarting the tomcat service the script runs perfectly for some time, after which this problem is again persists.

How do i solve this problem ?

EDIT : Java code :

String executableScript = <path to script>/script.sh;
String line;
String output;
String[] command = {executableScript};
Process process = Runtime.getRuntime().exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = input.readLine()) != null) {
    output.append(line);
}
process.waitFor();
input.close();
1
  • Can you show the relevant Java code as well? Commented May 15, 2014 at 9:07

2 Answers 2

1

The error script.sh: line 3: ifconfig: command not found means that the shell (launched by tomcat to run the command) is searching its PATH to find the ifconfig program, and it's not finding it. The value of the shell's PATH variable came from Tomcat, which would have gotten it from one of three places:

  1. If Tomcat was started by a system utility (/etc/init.d or systemd for example) then tomcat would get its path from there.

  2. If tomcat was started by you running a command from your terminal, then tomcat probably got its path from your environment.

  3. Tomcat (or any java class running inside tomcat) could change its PATH after startup.

If you're finding that the script always fails when tomcat was launched by a system utility, and it always works when tomcat was started from your terminal, then the issue is #1. The PATH which tomcat is getting from the system doesn't include the directory containing ifconfig.

If you're finding that the script works for a while after starting tomcat, and then it starts failing until you restart tomcat, then something within tomcat is changing tomcat's PATH environment variable. Tomcat itself probably doesn't contain any code to do that, so you should look at the applications which you have deployed within tomcat.

Sign up to request clarification or add additional context in comments.

Comments

1

Use the full path to ifconfig in your script, which is typically:

/sbin/ifconfig

When running from the tomcat service it will inherit the runtime environment of the service, such as $PATH.

5 Comments

but how does this explain that when I restart tomcat service it is able to find the ifconfig command ?
Also is the path /sbin/ifconfig same for all the flavours of Linux ?
That depends upon how the "service" was first started, and how you restarted it. I don't know about the second question as I've only ever used the RHEL/CentOS/Fedora flavours of linux.
@Pratham When you restart tomcat, it inherits your PATH and other environment variables.
@Kenster That's what the problem is. I don't change the PATH variable or any other environment variable when i restart tomcat. Tomcat just doesn't pick the PATH '/sbin' for the first time. But WHY ? Also I could find no explanation as to why after sometime it doesn't find the ifconfig command again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.