5

I'm trying to make a function in my bashrc that would allow me to launch any command and automatically disown it.

e.g. launch ./myprogram or launch xdg-open myfolder

I've been used to do that many times command ; Ctrl+Z ; bg ; disown and would like to simply create a shortcut of these steps.

However I don't know how to embed the action of Ctrl+Z in a bash script. I've seen that its action is SIGTSTP, but I'm really lost as to how incorporate that in a bash function.

2 Answers 2

6

You can run the command in background directly instead of stopping it and then running it in the background. Use the &:

$ cat > launch
#! /bin/bash
"$@" & disown

Ctrl + d

$ chmod u+x ./launch
5
  • I have a similar script, but also redirect stdin/stdout/stderr to /dev/null so that it doesn't try to block on input or spew debug info into my terminal.
    – root
    Commented Oct 5, 2019 at 15:40
  • ... I loathe myself. i knew about &, even knew I had to do that to replace bg but didn't realize it also effectively bypasses the need for ctrl+z. Thank you.
    – Atralb
    Commented Oct 5, 2019 at 15:42
  • You basically replace @choroba's second line with: < /dev/null "$@" &> /dev/null & disown
    – root
    Commented Oct 5, 2019 at 15:56
  • @root I had looked for it in the mean time and made the function in my answer below. Can you confirm that it does the same thing ? Or is my script imperfect ?
    – Atralb
    Commented Oct 5, 2019 at 16:13
  • You don't need to disown since starting a background process from a script constitutes a double fork. Commented Oct 5, 2019 at 16:42
1

For posterity and othe people passing by, here is the bash function I made :

 launch()
{
"$@" > /dev/null 2>&1 & disown
}
  • "$@" takes every arguments given in the prompt as one
  • > /dev/null 2>&1 redirects every output (stout and stderr) to dev/null which effectively delete them automatically, so that it doesn't appear on the shell
  • & runs the command in background, meaning it will let you input other commands in the shell
  • disown , as the name implies will lake it so that the process is no longer bound to the shell and you cans safely close the shell without it closing the process at the same time.
4
  • This does not work because the "current job" disown acts on may not be the job you think it should be. For example, if we type sleep 100 into the terminal, press Ctrl+Z, and type launch sleep 200, the first job is disowned, not the second job. However, launch() ( "$@" & disown ) does work because job control is not shared with the subshell. Commented Jul 5, 2023 at 15:19
  • Well the issue you talk about stems precisely from the fact of not using this script lol. So this is actually irrelevant theoretical nitpicking that is in practice an absolute non issue. I've been using this script for 5 years with hundreds of different executables. It works absolutely as intended, and without any output leaking (while some programs do if we do it manually like described in the post). I never Ctrl+Z anymore, simply because I never have to with this. However, thanks for the interesting note.
    – Atralb
    Commented Sep 16, 2023 at 23:41
  • I suppose it works if we exclusively use Ctrl+Z or launch, but that makes the command less robust against user slip-ups. There's also a more practical situation in which we would be tempted to mix the two commands: vim foo.py to edit some script, Ctrl+Z to return to the shell, then launch python foo.py to run the script disowned Commented Sep 19, 2023 at 20:50
  • I guess the reason you would want to Ctrl Z your vim session is to keep the undo history. I use a persistent undo dir in vim so I can always exit vim, run the program then reopen the file without losing the history (but we're beginning to stray from the original question here). All in all, I almost never have to use Ctrl+Z anymore since I have implemented launch. I can't even remember the last time I did.
    – Atralb
    Commented Oct 4, 2023 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.