2

I'm using the the following function to fork commands in my Turtle script:

forkCommand shellCommand = do
  pid <- inshell (shellCommand <> "& echo $!") empty
  return $ PID (lineToText pid)

The reason for doing this is because I want to get the PID of the forked process that I'm running.

The issue is that the command I'm ruining isn't streaming any stdout. For example you could set shellCommand to:

"python -c \"print('Hello, World')\""

and you won't see the print occur.

FintanH
  • 110
  • 11
  • I'm not sure what `PID` is doing, but if you define say `test cmd = view $ inshell (cmd <> " & echo $!") empty` and run say `test "cd ."` and `test "ls"` and the python, you see the process id is the first of potentially many lines streamed. I think you will have trouble recovering just the first line from a `Shell`. – Michael Apr 02 '17 at 22:52
  • Thanks for the reply @Michael! PID was just a newtype wrapper around Text. I ended up using `System.Process` particularly `spawnCommand` and then this answer http://stackoverflow.com/a/27388709/3982150 showed me how to get the PID. – FintanH Apr 02 '17 at 23:01
  • 1
    You can for example make a list of the outputs with say `fold some_shell Control.Foldl.list` in IO and then examine the list, but of course the process will be over by the time you get the list. – Michael Apr 02 '17 at 23:04
  • Btw, your program is the same as `fmap (PID . lineToText) $ inshell (shellCommand <> "& echo $!") empty` -- it makes each streamed line into something of the form `PID txt` – Michael Apr 02 '17 at 23:08
  • Ah, the fold idea isn't too bad! My real command would be a long running server rather than a hello world program :D I'll see how far I get with my current solution first. Thanks for the help and pointers! – FintanH Apr 03 '17 at 08:56

0 Answers0