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

Monks:

I'm trying to write a kick-off script for a program. I'll be sticking it in my crontab, but from time to time, I'll have to restart it manually, presumably from just copying from the crontab. What's worse, is that otherp eople will be doing the same thing, so I need to make it rather friendly.

I want the users to just run the script with it's arguments, and have them returned back to the prompt .I figured it would be easier to just have exec run the program, sending it to nohup. That works fine, but I do not get my prompt back, even if I run something like
exec("nohup", "/path/to/prog", "args", "&")
In fact, it seems that it passes & as an argument, as I can see it when I grep out the process.

Conversely, running the script with an ampersand appended doesn't give me my prompt back either.

Reading up, I see that exec replaces the currently running perl with whatever program exec kicks off. That's all fine and dandy, but I don't know what it means for me in the long run.

Thanks

Replies are listed 'Best First'.
Re: Starting a process and dying...
by ikegami (Patriarch) on Dec 08, 2004 at 05:09 UTC
    You need to do the "&" yourself by calling fork then calling exec in the child. Refer to perlipc. I think IPC::Run can hide some of the details.
Re: Starting a process and dying...
by sgifford (Prior) on Dec 08, 2004 at 05:26 UTC
    When you give a list to exec, it treats all of the function arguments as program arguments. Instead, pass it a string:
    perl -e 'exec("nohup sleep 10 &")'
    behaves how you'd expect.

    See 370911 for some more Perlish approaches.

      You know, I was reading that earlier today... for some reason I thought I needed to pass it a list. Not sure why, but you know, it's been a long day, and weirder things have happened. Cheers for both the answers -- quite informative.
        If you don't pass it as a list, you're asking the shell to interpret the line, which means 1) You're running an extra program (which is probably no biggie), and 2) you need to be wary of injection attacks. Be sure to validate any text sent as arguments to the child.