8

I'd like to always run a fish script in the background even if the user doesn't specify that.

In bash, this can be done by surrounding the script with ( at the start and ) & at the end.

Is there anyway for a fish script to run itself in the background?

  • 1
    Please [edit] your question and explain a bit more clearly. When should the script be launched? At boot time? After the user logs in? Every time a terminal is opened? What user should run the script? Also, all you need to send a command to the background is command &, the $(command) & syntax you describe is something else (and makes little sense). – terdon Jun 19 '16 at 18:34
  • If the script is foo.fish, then I intended for the script to run in the background when the user executes ./foo.fish. Surrounding multiple lines with () runs them in a subshell and & runs them in the background. Anyway, feel free to ignore this question as I've found out that it's not possible: https://github.com/fish-shell/fish-shell/issues/238 – user175817 Jun 19 '16 at 18:38
  • Well, yes, you can use a subshell, it's just not needed (and sorry, I thought you were using command substitution there). Anyway, you can do this in fish as well, just run ./foo.fish &. The post you linked to is about sending functions to the background. We might be able to help you but you need to give us a specific example. – terdon Jun 19 '16 at 18:43
  • The goal is to run the script in the background even if the user doesn't run ./foo.fish &. So even if the user runs ./foo.fish, the script should run in the background. The reason is because the script is invoked from an openvpn config file with the line ipchange foo.fish which doesn't support any arguments and cannot use &. – user175817 Jun 19 '16 at 18:51
  • Ah, OK, now I get it. Please [edit] your question and add these details though. Would using two scripts be acceptable? One that actually does the work (foo.fish) and one, bar.sh, that calls it as a background process: foo.fish &. You then have your user run bar.sh instead of foo.fish. – terdon Jun 19 '16 at 18:53
  • Job control proper is at the level of the calling shell. Would you like the script to continue running while returning a shell prompt to the caller? – Jeff Schaller Jun 19 '16 at 23:21

1 Answers1

11

fish does not fork to execute subshells, so it is not yet possible to run fish script in the background - see https://github.com/fish-shell/fish-shell/issues/563

A hackish workaround is to invoke fish again, like so:

#!/usr/local/bin/fish
fish -c 'sleep 5 ; echo done' &