How do I kill a spawned process?

Say I opened a video like this:

f = "path2video.file"
cmd = is_windows() ? `start $f` : is_linux() ? `xdg-open $f` : is_apple() ? `open $f` : error("Unknown OS")

How can I kill this process from within Julia later on?

1 Like

Since none of those commands return an handle to the launched process, the answer is “you can’t”

I think you can use stream, proc = open(cmd) and then call kill(proc). Does that work for your situation?

1 Like

kill(proc) returns 0 but the (in this case) video-player stays open and doesn’t close, even when I exit Julia…

I guess I wasn’t specific enough. Robin is correct that open will return a handle that you could later kill. However, the system programs listed (xdg-open, start, open) all fork again to run as daemons so that they cannot be killed (by design).

ahhhh… OK, thanks…
No way around that I guess? I’m using these to start a video of unknown (and often obscure) codec, but maybe there is a better way to do this…

How about using “xdg-mime query” to find the player, and then launching it directly?

1 Like

That’s great for Linux, but I need this to work cross-platform. But you are right, I’ll search for how to find the default application for different file types across all three major platforms. Thanks!

Wah…! This is pretty involved when it needs to be cross-platform. I’m starting to wonder if there isn’t a better solution to this problem.
My main options are:

  1. somehow identify which program runs which mime.
  2. start playing videos by opening the files with the most obvious player per platform (windows media player for Windows).
  3. somehow identify and kill processes that were spawned from xdg-open $f (and the Windows and Mac and Windows variations).

All three options have their disadvantages: #1 seems really hard on Windows, #2 is very vulnerable to errors (what if that ultra-strange codec file is played by some local proprietary program), #3 is apparently impossible.

I’d appreciate any insight you might have…!

4: render to a webbrowser using HTTP.jl

Some example projects that might help get you started: