pappajaz has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks, I am writing an admin interface to maintain some features of our server. I am writing a setuid wrapper that will startup a program with nohup and background it. The problem I have been having is that the program requires a <newline> after it is started sometimes. I have tried this numerous ways without luck.

example 1: hangs waiting for <ENTER/RETURN> key

system("nohup program &");
example 2: thought the problem was with nohup, still hangs
$SIG{HUP}='IGNORE'; exec("program &");
example 3: thought the problem was with the output.
system("nohup program >/dev/null &");
Sometimes the code works fine from the command line but hangs on the web browser everytime. I appreciate any advice anyone has.

Thank You,

---Adam

Replies are listed 'Best First'.
Re: hanging process
by sauoq (Abbot) on Oct 27, 2003 at 20:04 UTC
    example 1: hangs waiting for <ENTER/RETURN> key

    Unless the program you are calling via system is waiting for input, it is unlikely that this is actually hanging. You are probably just seeing nohup print a line like "nohup: appending output to `nohup.out'" to the terminal after the prompt and it is confusing you.

    Instead of hitting enter, just start typing another command like ls or something. You should see the prompt come back immediately. Alternatively, you could check with ps in another window to see if your process is running or not.

    Addendum: BTW, you would probably be better off writing your suid wrapper in C. Setuid scripts are problematic on some platforms.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: hanging process
by bluto (Curate) on Oct 27, 2003 at 20:54 UTC
    Redirecting output isn't goint to help if the program wants input. You probably want to make sure you've redirected anything in stdin/stdout/stderr that you don't want the child to inherit from the parent. Of course, nohup will do that for you for stdout/stderr. Try one of the following depending on how much input the program needs...
    # You need EOF on input... system("nohup program </dev/null >/dev/null 2>&1 &"); # You need 1 line of empty input... system("/bin/echo | nohup program >/dev/null 2>&1 &"); # You need infinite lines of empty input system("yes '' | nohup program >/dev/null 2>&1 &");
    ... all of which are kludgey and assume you don't care about error handling here (e.g. no nohup output; does program really start up ok?).

    bluto

      The program starts up okay. The program itself doesn't require any input. Its a "daemon" process but locks on occasion so I have written a kill script and this is the start script. The problem I have been having is that the internal shell from exec or system does not seem to exit after the program is finished executing. I did try typing in 'ls' and it executed as if the shell was still open. I have seen this appear often when backgrounding processes. This is where I got the <newline> comment.
      The web interface looks for output from nohup on stdout. Beyond the fact..the program works but hangs on the web interace. I have another program which calls 'ps -ef| grep <input>' and it shows the program running no problem.

      Thank you,

      Adam