Running detached screen session but without termination in the same bash

Here are already many questions like this, but every one recommends (a variant of) the following solution:

screen -dmS somename bash -c "/path/to/script; bash -l"

e.g. when the script ends, run an new bash.

Unfortunately, this isn't works for me, because

  • the script uses the $BASHPID variable for something
  • the script runs runs forever, e.g. need terminate it with CTRL-C
  • and after the termination i must have the same shell as runs the script (same $BASHPID)

So, the question is:

  • How to start the screen in detached mode (e.g. at boot) - normally this is done by the -dm
  • it must start the shell
    • best, if it can start it as "login" shell, e.g. what reads my .profile, but this can be overcome with the source ~/.profile in the script itself
    • the script start some other (binary) program what will run till CTRL-C
  • and after the termination of the script I must get the same bash (with the same $BASHPID (because need run another scripts what's depends on it)

In short need simulate the following interactive work:

  • start screen
  • echo $BASHPID
  • run-some-commands inside the screen (the last one runs until CTRL-C) - e.g. run the /path/to/script
  • detach (ctrl-a d)

After the above, later, I can do

  • reattach (screen -ARR)
  • CTRL-C
  • and can continue... with the same
  • echo $BASHPID #will produce the same as above

Any idea? (thinking about the -X but it not works, somewhere i probably making a mistake)


Solution 1:

It seems that the answer here is for your use case. In short:

bash --init-file <(echo "yourcommand")