Reputation: 141
How can I make a Java program running on Linux using Tanuki Wrapper (https://wrapper.tanukisoftware.com/) 'see' shell aliases?
The Java application is started by a 'Tanuki wrapper' script. The script is in turn started by nohup like this:
nohup ./start.sh &
Let's say I define an alias such as this in .bash_profile
:
alias ls='ls -al'
When the Java application executes an operating system program or command such as ls
, I expect ls -al
to be executed. However, ls
is executed -- the alias simply is not seen by the program. I understand aliases in Linux are typically not available in non-interactive shells (such as scripts or commands run by nohup).
So I tried modifications to force a login shell 'inside' the nohup command. But nothing works! The application doesn't see the aliases:
nohup bash -l -c "./start.sh" &
nohup bash -l -c "source .bash_profile; ./start.sh" &
I suspect the problem is Tanuki Wrapper. From what I understand, Tanuki Wrapper runs programs in a controlled environment that typically does not inherit the shell's interactive features or customizations.
How can I compel Tanuki wrapper to pickup settings from an interactive shell?
Upvotes: 0
Views: 39
Reputation: 123470
You can't. Shell aliases don't hook into anything in the system in any way. It's simply the shell doing a search&replace on your input before interpreting it. Java programs don't do that so are unaffected by aliases.
Instead, you can create a wrapper script and add it to your PATH. This lets any program find and invoke the script instead of the backing command:
$ cat bin/ls
#!/bin/bash
/bin/ls -la "$@"
$ chmod +x bin/ls
(no output)
$ cat Test.java
class Test {
public static void main(String[] args) throws Exception {
new ProcessBuilder("ls").redirectOutput(ProcessBuilder.Redirect.INHERIT).start();
}
}
If you don't change PATH, it uses the standard system ls
:
$ java Test.java
bin Test.java
If you do change it, the script is found first:
$ export PATH="$PWD/bin:$PATH"
$ java Test.java
total 16
drwxr-x--- 3 user group 4096 Sep 19 18:26 .
drwxr-xr-x 45 user group 4096 Sep 19 18:26 ..
drwxr-x--- 2 user group 4096 Sep 19 18:26 bin
-rw-r----- 1 user group 169 Sep 19 18:23 Test.java
Upvotes: 1