1
votes

i need to redirect stout and stderr in bash each to separate file. well i completed this command:

  ((/usr/bin/java -jar /opt/SEOC2/seoc2.jar 2>&1 1>&3 | tee --append /opt/SEOC2/log/err.log) 3>&1 1>&2 | tee --append /opt/SEOC2/log/app.log) >> /opt/SEOC2/log/combined.log 2>&1  &

which works fine running from a command line.

trying to put the very same command into bash script

 ...
 12 cmd="(($run -jar $cmd 2>&1 1>&3 | tee --append $err) 3>&1 1>&2 | tee --append $log) >> $combined 2>&1"
 ...
 30                 echo -e "Starting servis..." 
 31                 $cmd &
 32                 pid=`ps -eo pid,args | grep seoc2.jar | grep -v grep | cut -c1-6`
 33                 if [ ! -z $pid ]; then
 ...

leads to error like this:

   root@operator:/opt/SEOC2# seoc2 start
   Starting servis...
   /usr/local/bin/seoc2: line 31: ((/usr/bin/java: dir or file doesn't exist

tried to cover this command by $( ), ` ` etc but with no effect at all :(

any suggestion or advice would be very appreciated, playing around for hours already :/

thanx a lot Rene

1
Can you please post what you typed at the bash prompt to invoke the failing script, and show where you invoked it from (i.e. what was the current directory when you tired to run the script) - Josh Greifer
Do you need that entire pipeline in a single variable? The problem is that the quoted string causes the shell to not interpret the parentheses as shell syntax, but as literal characters in the string. Simplest would be to just write the command out on line 31. eval $cmd would work, but is not recommended. - chepner
@JoshGreife isn't what are you asking for in the last gray box from my original post? i.e. skript is located in /opt/SEOC2 but should be able to run from anywhere $err, $log, $combined are absolute path locations to /opt/SEOC2/log/ in this example i run it from /opt/SEOC2 - user1062331
ok, wasn't sure about the absolute locations. Thought it might be relative path. - Josh Greifer

1 Answers

2
votes

If you store the whole command line in a variable you have to use eval to execute it:

cmd="(($run -jar $cmd 2>&1 1>&3 | tee --append $err) 3>&1 1>&2 | tee --append $log) >> $combined 2>&1"
...
eval $cmd &