0

How can I start a perl script from within a perl script but without being part of the same thread of execution. Like forking and letting the original script continue execution?
I think system does not do that as I see in the same console the output of the other script

Jim
  • 18,826
  • 34
  • 135
  • 254
  • @ChrisHawkes: There is no solution in those links to execute and not replace the current script. Or execute and not wait – Jim Aug 18 '17 at 14:17
  • For your second script, do you only want to run it unless a certain condition occurs, or is it something that always needs to run? Also, do you not want to see output at all for that second script? – Cubis Aug 18 '17 at 16:08
  • @Cubis:I do not want to see any output of the second script. I just want to start it and continue/forget about it from the forking script point of view. And I want to run it in a specific area near the end of my first script – Jim Aug 18 '17 at 18:16
  • 1
    if you fork it, it returns 0 to the child process - with that, you can tell which process is the child, and have the child do a system call to your script and direct any output to /dev/null so it doesn't show any output on your console. – Cubis Aug 18 '17 at 18:55
  • @Cubis:That sounds simple enough. – Jim Aug 18 '17 at 18:58
  • Remember that if you do not want the child process to do anything after the system call you will want to close it, or make sure only the parent runs things after the if-child block – Cubis Aug 18 '17 at 19:04
  • @Cubis:One sec. What happens if the parent process finishes before the child which is what I expect? That would be a zombie process right? – Jim Aug 20 '17 at 10:03
  • Yeah, you may need the parent to call wait() in order to make sure it cleans up the child process when the child is done running, especially if you expect to run the main script a lot. However you must take into account the fact that the parent will not run anything else until the child process finishes. If all you were going for is to make sure the second script runs without any of its output being shown in the output of the first script, you may not even need to fork, just call system('/path/to/script2 > /dev/null 2>&1') this will redirect all output to essentially nothing – Cubis Aug 21 '17 at 02:55
  • @Cubis:I want script A to fork script B and script A to finish without caring about script B's output. From what you described it seems it is not possible is it? – Jim Aug 24 '17 at 18:40
  • If you absolutely must fork, and you want to prevent zombies, do something like this: parentCodeBeforeTheForkHere; $is_parent = fork(); if ($is_parent) { restOfParentCodeHere; wait(); # to prevent zombies } else { #childCodeHere } # end of script – Cubis Aug 24 '17 at 18:57
  • @Cubis: The `wait()` is what I want to avoid. I do not absolutely must fork. I just want a script to start another script and forget about it. So is it possible at all or not possible by forking or is there some library that supports this? – Jim Aug 24 '17 at 19:20
  • 1
    What I was trying to get at is that you can just put the wait() at the very end of your parent code. Your program was going to exit anyway at that point, so there's no real harm in your program waiting for the child to finish so it can clean up the zombie. However, check out this link that I saw today that doesn't even worry about waiting: it uses perl commands to turn off output before the child runs the exec: http://www.perlmonks.org/?node_id=1082332 – Cubis Aug 25 '17 at 18:18
  • @Cubis: Thanks for the thread. Seems that the key is: `close(STDOUT);close(STDIN);close(STDERR); exec('./fork-long-process-test-process');` But I didn't understand what is the side-effect of this. Why does this solve the issue? – Jim Aug 26 '17 at 08:36
  • If your whole point is to make sure no output from the second process goes to your console or other output from the first process, all you would need to do is make sure that the second process does not write anything to stdout, stdin, and stderr. The reason they were forking is to make sure you don't close those for the first process. If you put those commands inside the second process, you may not even need to fork at all. – Cubis Aug 28 '17 at 13:46
  • @Cubis: I thought that by closing everything, no inherited handlers of the parent process that exited would be in an undefined state making the second process behavior undefined too – Jim Aug 29 '17 at 20:44
  • I guess if you were trying to do something with the result of the exec within the parent process that would cause problems... I would personally make a simple test of the process using a dummy "parent script" and "child script" - have the child script close all outputs and then try to print something. Have the parent script print something before and after the fork, then run the parent script to see if it gets the behavior you want. – Cubis Aug 29 '17 at 21:53

0 Answers0