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

Hi All, New to Perl.. I have a script which is doing the following system "/usr/java/jdk-1.7.0_4-x86_64/bin/java -jar 'ProgramName"; This then produces a number of line's of output, then sits at the console screen waiting for a Ctrl+C to exit the command.. How can I suppress the message, I was looking at 'nohup' and two.. How do I get out of the Ctrl+C so that the script can continue... Or is there a better way than using 'system' Thanks.

Replies are listed 'Best First'.
Re: How to exit a system call
by RMGir (Prior) on Nov 08, 2013 at 13:00 UTC
    Does it actually wait for a ctrl-C, or would it also exit via ctrl-D?

    If so, that means it exits on the end of stdin, and your fix would be to simply run it as

    system "/usr/java/jdk-1.7.0_4-x86_64/bin/java -jar 'ProgramName' < /de +v/null"
    You might have to do something more complicated, as you suggested, but it's probably better to try this easy option first.

    Mike
Re: How to exit a system call
by kcott (Archbishop) on Nov 08, 2013 at 13:50 UTC

    G'day boardman411,

    Welcome to the monastery.

    Without seeing the context of the system call within your code, or knowing what ProgramName does, it's a little hard to give a definitive answer. I'm also unsure as to what you specifically want to do: suppress output? capture output? send a Ctrl-C? run in the background? some combination of these? something else?

    Here's a few suggestions, in no particular order:

    • You appear to be running on a *nix system. You can use 'command &' to run in the background; 'command > output_file' to redirect output; 'command > /dev/null' to discard output. There's all sorts of variations of these: consult your system manual page(s). Also, nohup is used to ignore a SIGHUP; it's unrelated to SIGINT (which is what Ctrl-C generates): man nohup should explain how this is implemented on your system.
    • Take a look at the system documentation. You'll find links to alternatives (e.g. qx and fork). Also read what it says about SIGINT.
    • "perlipc - Perl interprocess communication" describes a plethora of methods that might be of use to you.
    • For capturing output, Capture::Tiny may do what you want. Also, its SEE ALSO section lists many modules with similar or related functionality.
    • The Expect module is another possibility. In particular, see its "How do I send control characters to a process?" section.

    [Aside: A better question will get better answers. Take a look at the guidelines in "How do I post a question effectively?" to find out how to do this. Also, "How do I change/delete my post?" explains how to update what you've already posted.]

    -- Ken