Redirect stdout and stderror in child process

I have a problem when i try to create a log file from a daemon process using shell scripting in ubuntu 12. Ultimatly what i want to achieve is run a java/jar file from a script. After scourging the internet i found several solutions to do this, the one i choose is to create a startup script that calls a child script that contains all paramters required for the java program. From my startup script i run the following lines of code:

#!/bin/sh
DAEMON="/path/to/daemondir/daemon-file"
.....
local cmd="setsid $DAEMON >> $SERVICELOGFILE 2>&1 & echo \$! >$PIDFILE"
su "$USER" -c "$cmd" || return 1
.....

The DAEMON variable contains the script file that calls the actual java process with the following line:

#!/bin/sh
.....
exec $JAVA $JAVA_ARGS

Where $JAVA contains the location of the java command and the JAVA_ARGS are additonal arguments. If i call this daemon script directly and add the redirects to the java call everything works just as i would expect. But if i call the daemon script as a child script i do not get any results in my log file.

I do not know much about your daemon script, but: - on purpose - the Linux kernel does not support daemonized shell scripts. If your script really does that, then you need to get some code - C code for example - to run your script.

You can also achieve the something like same idea using the at command or the batch command.

Run your script automatically, right now, using at:

at  now <<!
$JAVA $JAVA_ARGS
!

Your environment variables will be preserved. Note - if you are creating a real daemon then stdout, stdin, stderr will have been closed. Before you run your script.

Basically i have this java application that i wish to run on reboot/by hand. The java application has its own logging but if for some reason it cannot run, e.g. wrong input parameters then it will crash and write its error to stdout and stderror.

I guess that is done for security reasons. It does make me wonder though why do commands such as start-stop-daemon exist then? And could i perhaps achieve the same using those commands?

EDIT:I also found some upstart examples but as i do not have that much experience with linux yet i am not sure if this will solve my problem.

I'm not a linux expert by any means, but that is news to me. I do know that linux does not support suid interpreted executables, but that has nothing to do with daemonization. Does linux prevent a setsid syscall from succeeding if the process is interpreted?

Regards,
Alister

alister - you are correct I left out the setuid and setgid. Thanks for the correction.

However if stdout or stderr are open there likely is problem with any daemonizing code,