With daemonize() throwing exception.


 
Thread Tools Search this Thread
Top Forums Programming With daemonize() throwing exception.
# 1  
Old 04-24-2009
With daemonize() throwing exception.

I have a function for daemonize() and then I call this to my main(). Here below is my main...

int main(int argc, char **argv)
{
daemonize();
try{
............
if(fail()) throw Exception(...)
.........
}
catch (const Exception& e) {
cout<< (e.toString());
return -1
}

here if try fails it is not throwing the error Exception. I tried by commenting the daemonize() call. Then they throw the error if try fails.
I could not understand why the daemonize() process does not allow the exceptions to throw.
I could not able to get clue, how to throw error without distrubing the daemonize();.
please help.
# 2  
Old 04-24-2009
Can you show us you daemonize() code?
# 3  
Old 04-25-2009
Here is my daemonize code:

void daemonize()
{
fclose(stderr);
fclose(stdin);
fclose(stdout);


switch( fork() ){
case 0:
break;
case -1:
throw FatalException(__FILE__, __LINE__);
default:
_exit(0);
}
setsid();

switch( fork() ){
case 0:
break;
case -1:
throw FatalException(__FILE__, __LINE__);
default:
_exit(0);
}

int fd;
fd=open("/dev/null", O_RDONLY);
if (fd != 0)
dup2(fd,0);
fd=open("/dev/null", O_WRONLY);
if (fd != 0) {
dup2(fd,1);
dup2(fd,2);
}
}
# 4  
Old 04-25-2009
You can't use fclose on stdin, stdout, or stderr. And there's no point in closing them when you're duplicating over them anyway, so just remove those altogether.

This likely isn't related to the error, but open() does not return 0 on error, it returns -1. Check if fd<0.

You should also close the original fd of anything you're duplicating once you're done copying it.
# 5  
Old 04-28-2009
Thanks. This helped.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For Loop throwing error

Hello Gurus, I am writing one script at linux. The logic is There is a find command which will find some specific files daily and store at a variable Then echo that variable . Now when I am trying to read the variable by using for loop it is throwing error as below:cat: CKDT.dat: No such... (5 Replies)
Discussion started by: pokhraj_d
5 Replies

2. Shell Programming and Scripting

Function throwing an error

Hi All I have two shell scripts where the second is getting invoked from the first. E.g. test1.sh and test2.sh Within test1, the code is something like this: #!/bin/bash . test2.sh usage() { echo "..." echo "....." } SRC=$1 DEST=$2 case "$3" in tran) doTran ;; *)... (7 Replies)
Discussion started by: swasid
7 Replies

3. Shell Programming and Scripting

Monitor logs for exception and if exception come then sent an email

Hi Folks, please advise , I have logs generated on unix machine at location /ops/opt/aaa/bvg.log , now sometimes there come exception in these logs also, so I want to write such a script such that it should continuously monitor these logs and whenever any exception comes that is it try to find... (3 Replies)
Discussion started by: tuntun27272727
3 Replies

4. Shell Programming and Scripting

awk--throwing an error

Hi all, I have below code sqlplus -s ext/exo@TIS << EOF whenever sqlerror exit failure rollback; set echo off set head off set serveroutput on set termout on set trimspool on SPOOL $SPOOL_FILE select 'ROWS '|| ' '||decode((count(Part_no)),0,'With greater values not... (3 Replies)
Discussion started by: Kiransagar
3 Replies

5. Shell Programming and Scripting

Tr--translate is throwing an error

Dear all, I would like to count the no;of word "INFORMATION" in a file called alt.lst and output to a unix variable INFORMATION.so to do this I wrote the below code INFORMATION=echo 'INFORMATION' | tr -cs 'A-Za-z' '\n' < /app/tisq005/01/home/tisq005b/scripts/alt.lst | grep -c "INFORMATION"... (2 Replies)
Discussion started by: Kiransagar
2 Replies

6. UNIX for Advanced & Expert Users

Problem on throwing sftp in nohup

Hi, but it is possible to effect a sftp in??? thanks thousand Germanico ---------- Post updated at 07:01 AM ---------- Previous update was at 05:51 AM ---------- Hi, but it is possible to effect a sftp in nohup mode??? (2 Replies)
Discussion started by: GERMANICO
2 Replies

7. Shell Programming and Scripting

for loop throwing an error

Hi Guys, I am trying a simple for loop which is throwing an error. Below is my code: #/bin/sh set -A array "are" "you" "there"; for ( i = 0 ; i < ${#array} ; i++ ) do echo ${array} done I am getting this error tci001wasd02 $ sh -vx array_for.sh #/bin/sh set -A array "are"... (6 Replies)
Discussion started by: mac4rfree
6 Replies

8. Shell Programming and Scripting

bc throwing coredump

Hi Gurus, I tried bc 1000 % 10 on tcsh and ksh and its throwing a core dump on a sun solaris machine. uname -a SunOS azote 5.9 Generic_118558-39 sun4u sparc SUNW,Ultra-4 Please let me know if you find anything. Thanks, Kinny (8 Replies)
Discussion started by: kinny
8 Replies

9. UNIX for Advanced & Expert Users

daemonize a process using ksh

I'm trying to create daemon processes with ksh as follows: function start { # start script as co-process and pass an argument ./1.ksh $1 |& # print pid print $! # move the file descriptors of the co-process to 4 and 5 exec 4>&p exec 5<&p # then close... (1 Reply)
Discussion started by: rein
1 Replies

10. Shell Programming and Scripting

sccs get -ek command is throwing err

Hi, get -ek s.Communicator.java The difference between the current date and the creation date of the newest delta in the SCCS file is greater than 1 year. Make sure that the system date is set correctly. This message is only a warning. (co11) 14.3 ... (0 Replies)
Discussion started by: Shivaprasad BS
0 Replies
Login or Register to Ask a Question