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)


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

bash --init-file <(echo "yourcommand")
  • Trying the above as: screen -L -dmS name bash --init-file <(echo "pwd;ls") (note the -L` logging) i got in the screenlog.0 the following: bash: /dev/fd/63: Bad file descriptor, and the commands aren't executed.(The child doesn't got the parent's redirection - the redirection is processed by the screen itself). Anyway, upvoting and accepting this, because it pointed me to the solution. --init-file /real/file/with/bash/commands works nicely. Thank you. – jm666 May 25 '15 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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