Redirect stdout and stderror in child process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirect stdout and stderror in child process
# 1  
Old 07-08-2013
Java 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:

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:

Code:
#!/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.
# 2  
Old 07-08-2013
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:
Code:
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.
# 3  
Old 07-08-2013
Quote:
I do not know much about your daemon 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.

Quote:
but: - on purpose - the Linux kernel does not support daemonized shell scripts.
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.

Last edited by Narev; 07-08-2013 at 11:46 AM..
# 4  
Old 07-08-2013
Quote:
Originally Posted by jim mcnamara
the Linux kernel does not support daemonized shell scripts.
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
# 5  
Old 07-08-2013
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,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bizzare behavior on redirect of stdout

Oracle Linux 5.6 64-bit (derivative of RHEL) Dear Ann Landers, This is about as bizarre as anything I've ever seen. I have a little test script I've been working with. When I redirect stdout to a file, no file. Make a copy of the script to another name. Execute it and redirect stdout, and... (4 Replies)
Discussion started by: edstevens
4 Replies

2. Shell Programming and Scripting

Automatically send stdout and stderror to a file as well as to the screen, but without using tee

Hi, I've been using the following commands in my automated scripts, to ensure that all text output is sent to a log file instead of to the screen: exec 1>>$SCRIPT_LOG_FILE exec 2>>$SCRIPT_LOG_FILE However, I've now discovered that the system used for automating the script executions... (4 Replies)
Discussion started by: confusedAdmin
4 Replies

3. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

4. Red Hat

Redirect STDOUT and STDERR of chsh

EDIT: Nevermind, figured it out! Forgot to put backslashes in my perl script to not process literals! Hi everyone. I am trying to have this command pass silently. (no output) chsh -s /bin/sh news Currently it outputs. I've tried.... &> /dev/null 1> /dev/null 2>&1 /dev/null 1>&2... (1 Reply)
Discussion started by: austinharris43
1 Replies

5. Shell Programming and Scripting

redirect STDOUT to a file in a subshell

Hi, I would like to avoid re-directing line by line to a file. What is the best way to re-direct STDOUT to a file in a subshell? Thanks in advance. Cheers Vj (1 Reply)
Discussion started by: tnvee
1 Replies

6. Programming

Controlling a child's stdin/stdout (not working with scp)

All, Ok...so I know I *should* be able to control a process's stdin and stdout from the parent by creating pipes and then dup'ing them in the child. And, this works with all "normal" programs that I've tried. Unfortunately, I want to intercept the stdin/out of the scp application and it seems... (9 Replies)
Discussion started by: DreamWarrior
9 Replies

7. Programming

how to redirect back to stdout

In my program, I am using library provided by other. In the library, the cout/cerr is redirected to a file (the file path is known). After I call some methods in the library, I get one side-effect --> The cout/cerr in my own program is also directed to the file. So how can I to redirect... (5 Replies)
Discussion started by: princelinux
5 Replies

8. Programming

redirect stdout

hello again! i use dup2 to redirect stdout. I run what i want, now i want undo this redirection. how can i do that? thanx in advance (7 Replies)
Discussion started by: nicos
7 Replies

9. Shell Programming and Scripting

How to redirect stderr and stdout to a file

Hi friends I am facing one problem while redirecting the out of the stderr and stdout to a file let example my problem with a simple example I have a file (say test.sh)in which i run 2 command in the background ps -ef & ls & and now i am run this file and redirect the output to a file... (8 Replies)
Discussion started by: sushantnirwan
8 Replies

10. Shell Programming and Scripting

Redirect stdout and stderr

How can I redirect and append stdout and stderr to a file when using cron? Here is my crontab file: */5 * * * * /dir/php /dir/process_fns.php >>& /dir/dump.txt Cron gives me an 'unexpected character found in line' when trying to add my crontab file. Regards, Zach Curtis POPULUS (8 Replies)
Discussion started by: zcurtis
8 Replies
Login or Register to Ask a Question