3

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)

9
  • 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. Commented Oct 21, 2022 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; Commented Oct 21, 2022 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! Commented Oct 27, 2022 at 2: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; ' Commented Nov 5, 2022 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. Commented Nov 7, 2022 at 6:12

2 Answers 2

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 @@ &
0

As mentioned in one of the comments, the most straight-forward solution would be to preface the flatpak command with setsid, i.e.:

#!/bin/bash

nohup setsid flatpak run org.libreoffice.LibreOffice >/dev/null 2>&1 &

setsid runs the command in a new session (process group). Obviously flatpak contains some logic to watch the process group it was started from and ends its container when the process group goes away. Using setsid it runs in its own group and is thusly immune from the script that started it.

The other ways mentioned in other comments seem to have similar effects (i.e., running it in a sub-bash which keeps running and is itself nohup'ed), but setsid seems the most direct solution, and also the one that goes straight to the root cause of the problem.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.