سؤال

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();
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top