2

I've always successfuly used an ampersand to start an application in the background from within a script. Such background applications seem to be detached from the script that started it, meaning that when I terminate the script, the applications keep running.

I now have a script that first starts a number of applications, some of them flatpaks, and then starts an endless while loop to monitor my ip address to check that the vpn is still working.

#!/bin/bash
start application 1 &
start application 2 &
start flatpak application 1 &
start flatpak application 2 &

while true do check ip address sleep 180 done

When I terminate the script with ctrl-C, the non-flatpak applications keep running (as expected), but the flatpaks are terminated.

This, however, only happens if the script has this while loop: if I remove the while loop, then all applications keep running when I terminate the script, including the flatpaks.

So far, I've tried:

  • calling the applications with the ampersand:
/usr/bin/flatpak run --command=de.manuel_kehl.go-for-it de.manuel_kehl.go-for-it &
  • calling the applications with nohup:
nohup /usr/bin/flatpak run --command=de.manuel_kehl.go-for-it de.manuel_kehl.go-for-it &
  • calling disown after starting the application
/usr/bin/flatpak run --command=de.manuel_kehl.go-for-it de.manuel_kehl.go-for-it &
disown 

None of these approaches help.

Can someone explain why this happens? And is there a solution? I would like to keep all applications, including the flatpaks, running when I terminate the script.

tnx

(using bash on Linux Mint 19.3 MATE)

wayan
  • 91
  • No luck here either. I finally decided to make a function which I invoke from the terminal to launch flatpaks and disown. Closing the terminal does not close the flatpak applications. Posting function in next comment. I cannot post it in answers because it does not work when launched from a shell script. – RuntimeException Oct 21 '22 at 16:06
  • `function runapp() { if [ $1 == "geany" ]; then echo file to open [$2]; /usr/bin/flatpak run --command=geany --file-forwarding org.geany.Geany @@ $2 @@ 1>~/logs/geany.out 2>~/logs/geany.out &; disown; return 0; fi

    }export -f run; declare -f run;`

    – RuntimeException Oct 21 '22 at 16:06
  • @RuntimeException Thank you for replying! I experimented with your suggestion, but like you, I can't get it to work from a shell script. Oh well. Thanks anyway! – wayan Oct 27 '22 at 02:13
  • You might want to try bash -c '/usr/bin/flatpak run --command=de.manuel_kehl.go-for-it de.manuel_kehl.go-for-it &; disown; ' – RuntimeException Nov 05 '22 at 14:37
  • @RuntimeException Thanks! My system complained about the semicolon after the ampersand, i had to remove it. The flatpak now stays active after terminating the script, but still not when the script contains an endless loop like in the original post. – wayan Nov 07 '22 at 06:12
  • You may want to have two separate scripts. One script starts the flatpaks and exits. Then you start another script which loops and checks things. – RuntimeException Nov 07 '22 at 10:16
  • @RuntimeException Yeah, that's sort of what I'm doing now. Run the script and start the flatpaks manually. Thanks for your input! – wayan Nov 08 '22 at 14:11
  • @RuntimeException I still don't understand the inner workings, but https://unix.stackexchange.com/a/352923 has a working solution by preceeding the command with "setsid" and ending with "&", as in setsid /usr/bin/flatpak run --command=de.manuel_kehl.go-for-it de.manuel_kehl.go-for-it & – wayan Nov 19 '22 at 08:22
  • Great. Thanks for the info, I will keep that in mind in case I ever face such a thing with bash scripts and flatpaks. I think you may start the flatpak apps by invoking another bash script from the .bashrc itself, if that is what you want. Or start the script that loops from .bashrc using the bash -c ; disown; thing so that it starts in another shell and the .bashrc completes successfully. But anyway, if your problem is solved, then no need to bother. – RuntimeException Nov 30 '22 at 15:12

1 Answers1

0

Here's what I ended up doing to be able to get Thunderbird to run in my startup script:

/usr/bin/flatpak run --branch=stable --arch=x86_64 --command=thunderbird --file-forwarding org.mozilla.Thunderbird @@u %u @@ &