Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Close script with open system call

by Phweda (Initiate)
on Feb 27, 2020 at 12:24 UTC ( [id://11113484]=perlquestion: print w/replies, xml ) Need Help??

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

Script ends when it opens its logfile by a call to 'system'. Even with explicit 'exit' the script keeps running. Assume it is waiting for the system call to terminate? In this case want to force the script to terminate.

my $logFileName = $rootDIR . "/fmlog.txt"; open $OUTPUTFILE, "> $logFileName" or die "Died trying to open the out +put file $!"; # Processing files and logging results close ($OUTPUTFILE); # Open Log file for user system($logFileName); exit;
Script stays open until text application is closed or until a ctrl-c Thanks Phweda

Replies are listed 'Best First'.
Re: Close script with open system call
by kennethk (Abbot) on Feb 27, 2020 at 13:10 UTC
    As documented in system:
    Does exactly the same thing as exec, except that a fork is done first and the parent process waits for the child process to exit.
    So, if you don't want to wait for the child process to exit (e.g., someone to close the text editor), change that to an exec. It will change the executing process -- mind body and soul -- to be the running text editor, giving up control forever.

    You also can remove the exit in that case, too. Please note that exit is almost never what you actually mean to do if you don't need to return a status code. Just let the script roll off the end.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thanks all exec was what I needed.

Re: Close script with open system call
by Eily (Monsignor) on Feb 27, 2020 at 13:39 UTC

    If the issue is that you only want one process to be executing at the end of your script, or you want the end of script clean-up to be triggered exec should do fine.

    If you want to get back control in the console, then neither system nor exec will do what you want, as in the first case the process you started by calling the script will wait for the editor to close, and in the second the process will turn into the editor's, so by definition won't have stopped until the editor has stopped.

    On linux fork can be used like this:

    fork and exit; exec($command)
    Which will do what you want (a new process will be created, but the parent won't wait for it before exiting). On Windows I don't know... (it might work with the correct build of perl, it doesn't with my strawberry perl 5.30). You might want to redirect the editor's output as well, if you don't want messages to pop up in your console.

    Edit: a simple test to demonstrate (using linux quotes since fork isn't implemented in my Windows perl)

    perl -E 'fork and exit; exec q<perl -E "sleep 10; say 1">'
    You then have 10 seconds to try and execute other commands before the 1 is displayed.

Re: Close script with open system call
by 1nickt (Canon) on Feb 27, 2020 at 12:30 UTC

    Hi, maybe you want exec instead?


    The way forward always starts with a minimal test.
Re: Close script with open system call
by ForgotPasswordAgain (Priest) on Feb 27, 2020 at 13:36 UTC
    It's weird to try to execute a log file. Do you want to run $ENV{PAGER} on it or something?

      occam's razor in practice

Re: Close script with open system call
by NERDVANA (Deacon) on Feb 27, 2020 at 19:42 UTC
    I’m assuming you are on Windows if you are executing a log file and getting the result you expected. (having it open in notepad or whatever)

    The more official way to do this is with the ShellExecute Win32 API function. Looks like the Win32::FileOp module can do this. I believe the perl script will continue running without waiting for Notepad to exit, so you can end the perl script more gracefully than with exec.

    This was previously answered in Launching a data file in Win32 using the assciated application automatically

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11113484]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found