you are viewing a single comment's thread
view the rest of the comments
[–] 24 points 10 months ago* (7 children)

I have a script named d in my PATH and it contains this:

("$@" > /dev/null 2>&1 &)

It allows me to run any program in a fully detached state in a way that works even if the terminal that started the program closes, and it's as simple as d <command>.

  • source
  • hideshow 7 child comments
  • [–] 5 points 10 months ago (2 children)

    Shouldn't you end with & disown to fully detach?

  • source
  • parent
  • hideshow 2 child comments
  • [–] 3 points 10 months ago* (1 child)

    IIRC disown is a shell built-in command, so its use is a bit limited. Not sure if & is also a built-in, but I found disown to not work in some situations. Besides, it's shorter.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 2 points 10 months ago

    shell built-in command

    After looking into it a bit more, its at least a builtin for bash but is otherwise not POSIX. I guess nohup ... & would be the POSIX compliant equivalent, though still not a builtin.

    Its my understanding that & backgrounds, not necessarily detaches, a process -- if the parent process closes, I think the background tasks would still be wait()ed on, if only using &.

  • source
  • parent
  • [–] 3 points 10 months ago (2 children)

    How does this even work? I get the redirection part, but how is the command executed in a detached state?

  • source
  • parent
  • hideshow 2 child comments
  • [–] 4 points 10 months ago

    () creates a subshell, and & runs the command in background. The $@ means everything after the first argument, so the <command> is executed like a normal command. I am not sure why this works, but it has worked more consistently than nohup, disown, and it's a lot shorter than most other solutions.

  • source
  • parent