How can I get a confirmation before exiting screen?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












How can I get a confirmation before I exit screen (when typing exit on the command-line). Is this possible?







share|improve this question






















  • Screen exits when the last program/window in it exits, so I believe your question boils down to having your shell prompt before exiting if it is the last screen window....?
    – Jeff Schaller
    Apr 11 at 10:23














up vote
3
down vote

favorite












How can I get a confirmation before I exit screen (when typing exit on the command-line). Is this possible?







share|improve this question






















  • Screen exits when the last program/window in it exits, so I believe your question boils down to having your shell prompt before exiting if it is the last screen window....?
    – Jeff Schaller
    Apr 11 at 10:23












up vote
3
down vote

favorite









up vote
3
down vote

favorite











How can I get a confirmation before I exit screen (when typing exit on the command-line). Is this possible?







share|improve this question














How can I get a confirmation before I exit screen (when typing exit on the command-line). Is this possible?









share|improve this question













share|improve this question




share|improve this question








edited Apr 11 at 10:23









Jeff Schaller

31.1k846105




31.1k846105










asked Apr 11 at 9:11









Taapo

1182




1182











  • Screen exits when the last program/window in it exits, so I believe your question boils down to having your shell prompt before exiting if it is the last screen window....?
    – Jeff Schaller
    Apr 11 at 10:23
















  • Screen exits when the last program/window in it exits, so I believe your question boils down to having your shell prompt before exiting if it is the last screen window....?
    – Jeff Schaller
    Apr 11 at 10:23















Screen exits when the last program/window in it exits, so I believe your question boils down to having your shell prompt before exiting if it is the last screen window....?
– Jeff Schaller
Apr 11 at 10:23




Screen exits when the last program/window in it exits, so I believe your question boils down to having your shell prompt before exiting if it is the last screen window....?
– Jeff Schaller
Apr 11 at 10:23










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










I approached this by masking the exit command with a function; the function checks to see if you're within screen, and if you're the only child process left of that screen process.



exit() 
if [[ "$(ps -o pid= --ppid "$(ps -o ppid= -p "$$")"


You would have to include that function as part of your (bash) profile, e.g. ~/.bash_profile. When starting screen, it will (unless told otherwise), start an instance of your $SHELL. That shell will be a child of a screen process. When exiting from a child shell, the above code checks to see how many processes are children of the current shell's parent process. From the inside out:




  • $(ps -o ppid= -p "$$") -- asks for the parent PID (adding the = suppresses the header line) of the current process ($$)


  • $(ps -o pid= --ppid ... | wc -l) -- asks for the list of PIDs (again without the header) whose parent PID is our parent, then counts the number of lines of output

If it looks like we're the last child process of a screen session, it prompts for confirmation; if the response begins with the letter y, the function calls the "real" exit command to exit the shell; otherwise, the function ends without exiting the shell.



If we're not the last child process, the function goes ahead and exits normally.



A couple notes as I developed this:



  • I initially had more tests in the if line to see if we are within a screen session, including seeing if STY is populated and for SHLVL being greater than 1. screen sets STY, and bash will increment SHLVL, but neither of those variables are read-only, so the test is not strong enough to be useful.


  • screen also sets a WINDOW variable, but checking it for 0 is not reliable; you could open two windows and then close window 0, leaving window 1 as the last window.


  • Entering EOF (usually Control+D) will, by default, cause the shell to immediately exit, bypassing this wrapper function. The best workaround I know would be to set the variable IGNOREEOF to some non-zero value; that will only delay the shell's inevitable exit, though.


Because I used so many bash- (and GNU procutils-) specific features above, I wanted to also provide a POSIX-conformant solution. The ps line changes to a ps ... | grep -c scheme in order to capture the number of processes with a specific parent PID. The other change is to re-work the read -p into a separate prompt and read.



exit() grep -c "^ *$parent$" )" -eq 1 ]
then
printf "Warning: you are in the last screen window; do you really want to exit? (y/n) "
read REPLY
case $REPLY in
(y*) command exit "$@" ;;
esac
else
# not within screen at all, or not within the last screen window
command exit "$@"
fi






share|improve this answer





























    up vote
    0
    down vote













    I assume you are using bash.



    are_u_sure()
    read -n1 -p "Are you sure to exit? [y/N] "
    [ "$REPLY" != y ] && echo; history -a; bash;


    trap are_u_sure EXIT


    Add this to your ~/.bashrc, and then whenever you try to exit bash (type exit or ^D), you will be prompted. And you'll never quit unless you press 'y'.



    This way, we don't have to stick to the screen magic since we can do it bash-wide.



    As this approach is extremely simple, it does have a flaw, you will lose the variable assignments you typed in before. But may be we can work on that. You can modifiy this code to fit your needs anyway.






    share|improve this answer






















    • This technically quits bash but spawns a new interactive bash shell before it quits, leaving the original shell hanging in its EXIT signal handler until the new shell exits. Unconventional, but I suppose it works.
      – Kusalananda
      Apr 16 at 6:30










    • Yes. It works fine for me.
      – dotc
      Apr 16 at 6:40










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f436959%2fhow-can-i-get-a-confirmation-before-exiting-screen%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    I approached this by masking the exit command with a function; the function checks to see if you're within screen, and if you're the only child process left of that screen process.



    exit() 
    if [[ "$(ps -o pid= --ppid "$(ps -o ppid= -p "$$")"


    You would have to include that function as part of your (bash) profile, e.g. ~/.bash_profile. When starting screen, it will (unless told otherwise), start an instance of your $SHELL. That shell will be a child of a screen process. When exiting from a child shell, the above code checks to see how many processes are children of the current shell's parent process. From the inside out:




    • $(ps -o ppid= -p "$$") -- asks for the parent PID (adding the = suppresses the header line) of the current process ($$)


    • $(ps -o pid= --ppid ... | wc -l) -- asks for the list of PIDs (again without the header) whose parent PID is our parent, then counts the number of lines of output

    If it looks like we're the last child process of a screen session, it prompts for confirmation; if the response begins with the letter y, the function calls the "real" exit command to exit the shell; otherwise, the function ends without exiting the shell.



    If we're not the last child process, the function goes ahead and exits normally.



    A couple notes as I developed this:



    • I initially had more tests in the if line to see if we are within a screen session, including seeing if STY is populated and for SHLVL being greater than 1. screen sets STY, and bash will increment SHLVL, but neither of those variables are read-only, so the test is not strong enough to be useful.


    • screen also sets a WINDOW variable, but checking it for 0 is not reliable; you could open two windows and then close window 0, leaving window 1 as the last window.


    • Entering EOF (usually Control+D) will, by default, cause the shell to immediately exit, bypassing this wrapper function. The best workaround I know would be to set the variable IGNOREEOF to some non-zero value; that will only delay the shell's inevitable exit, though.


    Because I used so many bash- (and GNU procutils-) specific features above, I wanted to also provide a POSIX-conformant solution. The ps line changes to a ps ... | grep -c scheme in order to capture the number of processes with a specific parent PID. The other change is to re-work the read -p into a separate prompt and read.



    exit() grep -c "^ *$parent$" )" -eq 1 ]
    then
    printf "Warning: you are in the last screen window; do you really want to exit? (y/n) "
    read REPLY
    case $REPLY in
    (y*) command exit "$@" ;;
    esac
    else
    # not within screen at all, or not within the last screen window
    command exit "$@"
    fi






    share|improve this answer


























      up vote
      2
      down vote



      accepted










      I approached this by masking the exit command with a function; the function checks to see if you're within screen, and if you're the only child process left of that screen process.



      exit() 
      if [[ "$(ps -o pid= --ppid "$(ps -o ppid= -p "$$")"


      You would have to include that function as part of your (bash) profile, e.g. ~/.bash_profile. When starting screen, it will (unless told otherwise), start an instance of your $SHELL. That shell will be a child of a screen process. When exiting from a child shell, the above code checks to see how many processes are children of the current shell's parent process. From the inside out:




      • $(ps -o ppid= -p "$$") -- asks for the parent PID (adding the = suppresses the header line) of the current process ($$)


      • $(ps -o pid= --ppid ... | wc -l) -- asks for the list of PIDs (again without the header) whose parent PID is our parent, then counts the number of lines of output

      If it looks like we're the last child process of a screen session, it prompts for confirmation; if the response begins with the letter y, the function calls the "real" exit command to exit the shell; otherwise, the function ends without exiting the shell.



      If we're not the last child process, the function goes ahead and exits normally.



      A couple notes as I developed this:



      • I initially had more tests in the if line to see if we are within a screen session, including seeing if STY is populated and for SHLVL being greater than 1. screen sets STY, and bash will increment SHLVL, but neither of those variables are read-only, so the test is not strong enough to be useful.


      • screen also sets a WINDOW variable, but checking it for 0 is not reliable; you could open two windows and then close window 0, leaving window 1 as the last window.


      • Entering EOF (usually Control+D) will, by default, cause the shell to immediately exit, bypassing this wrapper function. The best workaround I know would be to set the variable IGNOREEOF to some non-zero value; that will only delay the shell's inevitable exit, though.


      Because I used so many bash- (and GNU procutils-) specific features above, I wanted to also provide a POSIX-conformant solution. The ps line changes to a ps ... | grep -c scheme in order to capture the number of processes with a specific parent PID. The other change is to re-work the read -p into a separate prompt and read.



      exit() grep -c "^ *$parent$" )" -eq 1 ]
      then
      printf "Warning: you are in the last screen window; do you really want to exit? (y/n) "
      read REPLY
      case $REPLY in
      (y*) command exit "$@" ;;
      esac
      else
      # not within screen at all, or not within the last screen window
      command exit "$@"
      fi






      share|improve this answer
























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        I approached this by masking the exit command with a function; the function checks to see if you're within screen, and if you're the only child process left of that screen process.



        exit() 
        if [[ "$(ps -o pid= --ppid "$(ps -o ppid= -p "$$")"


        You would have to include that function as part of your (bash) profile, e.g. ~/.bash_profile. When starting screen, it will (unless told otherwise), start an instance of your $SHELL. That shell will be a child of a screen process. When exiting from a child shell, the above code checks to see how many processes are children of the current shell's parent process. From the inside out:




        • $(ps -o ppid= -p "$$") -- asks for the parent PID (adding the = suppresses the header line) of the current process ($$)


        • $(ps -o pid= --ppid ... | wc -l) -- asks for the list of PIDs (again without the header) whose parent PID is our parent, then counts the number of lines of output

        If it looks like we're the last child process of a screen session, it prompts for confirmation; if the response begins with the letter y, the function calls the "real" exit command to exit the shell; otherwise, the function ends without exiting the shell.



        If we're not the last child process, the function goes ahead and exits normally.



        A couple notes as I developed this:



        • I initially had more tests in the if line to see if we are within a screen session, including seeing if STY is populated and for SHLVL being greater than 1. screen sets STY, and bash will increment SHLVL, but neither of those variables are read-only, so the test is not strong enough to be useful.


        • screen also sets a WINDOW variable, but checking it for 0 is not reliable; you could open two windows and then close window 0, leaving window 1 as the last window.


        • Entering EOF (usually Control+D) will, by default, cause the shell to immediately exit, bypassing this wrapper function. The best workaround I know would be to set the variable IGNOREEOF to some non-zero value; that will only delay the shell's inevitable exit, though.


        Because I used so many bash- (and GNU procutils-) specific features above, I wanted to also provide a POSIX-conformant solution. The ps line changes to a ps ... | grep -c scheme in order to capture the number of processes with a specific parent PID. The other change is to re-work the read -p into a separate prompt and read.



        exit() grep -c "^ *$parent$" )" -eq 1 ]
        then
        printf "Warning: you are in the last screen window; do you really want to exit? (y/n) "
        read REPLY
        case $REPLY in
        (y*) command exit "$@" ;;
        esac
        else
        # not within screen at all, or not within the last screen window
        command exit "$@"
        fi






        share|improve this answer














        I approached this by masking the exit command with a function; the function checks to see if you're within screen, and if you're the only child process left of that screen process.



        exit() 
        if [[ "$(ps -o pid= --ppid "$(ps -o ppid= -p "$$")"


        You would have to include that function as part of your (bash) profile, e.g. ~/.bash_profile. When starting screen, it will (unless told otherwise), start an instance of your $SHELL. That shell will be a child of a screen process. When exiting from a child shell, the above code checks to see how many processes are children of the current shell's parent process. From the inside out:




        • $(ps -o ppid= -p "$$") -- asks for the parent PID (adding the = suppresses the header line) of the current process ($$)


        • $(ps -o pid= --ppid ... | wc -l) -- asks for the list of PIDs (again without the header) whose parent PID is our parent, then counts the number of lines of output

        If it looks like we're the last child process of a screen session, it prompts for confirmation; if the response begins with the letter y, the function calls the "real" exit command to exit the shell; otherwise, the function ends without exiting the shell.



        If we're not the last child process, the function goes ahead and exits normally.



        A couple notes as I developed this:



        • I initially had more tests in the if line to see if we are within a screen session, including seeing if STY is populated and for SHLVL being greater than 1. screen sets STY, and bash will increment SHLVL, but neither of those variables are read-only, so the test is not strong enough to be useful.


        • screen also sets a WINDOW variable, but checking it for 0 is not reliable; you could open two windows and then close window 0, leaving window 1 as the last window.


        • Entering EOF (usually Control+D) will, by default, cause the shell to immediately exit, bypassing this wrapper function. The best workaround I know would be to set the variable IGNOREEOF to some non-zero value; that will only delay the shell's inevitable exit, though.


        Because I used so many bash- (and GNU procutils-) specific features above, I wanted to also provide a POSIX-conformant solution. The ps line changes to a ps ... | grep -c scheme in order to capture the number of processes with a specific parent PID. The other change is to re-work the read -p into a separate prompt and read.



        exit() grep -c "^ *$parent$" )" -eq 1 ]
        then
        printf "Warning: you are in the last screen window; do you really want to exit? (y/n) "
        read REPLY
        case $REPLY in
        (y*) command exit "$@" ;;
        esac
        else
        # not within screen at all, or not within the last screen window
        command exit "$@"
        fi







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 16 at 12:33









        Kusalananda

        102k13199316




        102k13199316










        answered Apr 14 at 1:47









        Jeff Schaller

        31.1k846105




        31.1k846105






















            up vote
            0
            down vote













            I assume you are using bash.



            are_u_sure()
            read -n1 -p "Are you sure to exit? [y/N] "
            [ "$REPLY" != y ] && echo; history -a; bash;


            trap are_u_sure EXIT


            Add this to your ~/.bashrc, and then whenever you try to exit bash (type exit or ^D), you will be prompted. And you'll never quit unless you press 'y'.



            This way, we don't have to stick to the screen magic since we can do it bash-wide.



            As this approach is extremely simple, it does have a flaw, you will lose the variable assignments you typed in before. But may be we can work on that. You can modifiy this code to fit your needs anyway.






            share|improve this answer






















            • This technically quits bash but spawns a new interactive bash shell before it quits, leaving the original shell hanging in its EXIT signal handler until the new shell exits. Unconventional, but I suppose it works.
              – Kusalananda
              Apr 16 at 6:30










            • Yes. It works fine for me.
              – dotc
              Apr 16 at 6:40














            up vote
            0
            down vote













            I assume you are using bash.



            are_u_sure()
            read -n1 -p "Are you sure to exit? [y/N] "
            [ "$REPLY" != y ] && echo; history -a; bash;


            trap are_u_sure EXIT


            Add this to your ~/.bashrc, and then whenever you try to exit bash (type exit or ^D), you will be prompted. And you'll never quit unless you press 'y'.



            This way, we don't have to stick to the screen magic since we can do it bash-wide.



            As this approach is extremely simple, it does have a flaw, you will lose the variable assignments you typed in before. But may be we can work on that. You can modifiy this code to fit your needs anyway.






            share|improve this answer






















            • This technically quits bash but spawns a new interactive bash shell before it quits, leaving the original shell hanging in its EXIT signal handler until the new shell exits. Unconventional, but I suppose it works.
              – Kusalananda
              Apr 16 at 6:30










            • Yes. It works fine for me.
              – dotc
              Apr 16 at 6:40












            up vote
            0
            down vote










            up vote
            0
            down vote









            I assume you are using bash.



            are_u_sure()
            read -n1 -p "Are you sure to exit? [y/N] "
            [ "$REPLY" != y ] && echo; history -a; bash;


            trap are_u_sure EXIT


            Add this to your ~/.bashrc, and then whenever you try to exit bash (type exit or ^D), you will be prompted. And you'll never quit unless you press 'y'.



            This way, we don't have to stick to the screen magic since we can do it bash-wide.



            As this approach is extremely simple, it does have a flaw, you will lose the variable assignments you typed in before. But may be we can work on that. You can modifiy this code to fit your needs anyway.






            share|improve this answer














            I assume you are using bash.



            are_u_sure()
            read -n1 -p "Are you sure to exit? [y/N] "
            [ "$REPLY" != y ] && echo; history -a; bash;


            trap are_u_sure EXIT


            Add this to your ~/.bashrc, and then whenever you try to exit bash (type exit or ^D), you will be prompted. And you'll never quit unless you press 'y'.



            This way, we don't have to stick to the screen magic since we can do it bash-wide.



            As this approach is extremely simple, it does have a flaw, you will lose the variable assignments you typed in before. But may be we can work on that. You can modifiy this code to fit your needs anyway.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 14 at 10:21

























            answered Apr 14 at 8:18









            dotc

            163




            163











            • This technically quits bash but spawns a new interactive bash shell before it quits, leaving the original shell hanging in its EXIT signal handler until the new shell exits. Unconventional, but I suppose it works.
              – Kusalananda
              Apr 16 at 6:30










            • Yes. It works fine for me.
              – dotc
              Apr 16 at 6:40
















            • This technically quits bash but spawns a new interactive bash shell before it quits, leaving the original shell hanging in its EXIT signal handler until the new shell exits. Unconventional, but I suppose it works.
              – Kusalananda
              Apr 16 at 6:30










            • Yes. It works fine for me.
              – dotc
              Apr 16 at 6:40















            This technically quits bash but spawns a new interactive bash shell before it quits, leaving the original shell hanging in its EXIT signal handler until the new shell exits. Unconventional, but I suppose it works.
            – Kusalananda
            Apr 16 at 6:30




            This technically quits bash but spawns a new interactive bash shell before it quits, leaving the original shell hanging in its EXIT signal handler until the new shell exits. Unconventional, but I suppose it works.
            – Kusalananda
            Apr 16 at 6:30












            Yes. It works fine for me.
            – dotc
            Apr 16 at 6:40




            Yes. It works fine for me.
            – dotc
            Apr 16 at 6:40












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f436959%2fhow-can-i-get-a-confirmation-before-exiting-screen%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay