How can I get a confirmation before exiting screen?
Clash 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?
gnu-screen exit
add a comment |Â
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?
gnu-screen exit
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
add a comment |Â
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?
gnu-screen exit
How can I get a confirmation before I exit screen (when typing exit on the command-line). Is this possible?
gnu-screen exit
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
add a comment |Â
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
add a comment |Â
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 ifSTY
is populated and forSHLVL
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 for0
is not reliable; you could open two windows and then close window0
, leaving window1
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
add a comment |Â
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.
This technically quitsbash
but spawns a new interactivebash
shell before it quits, leaving the original shell hanging in itsEXIT
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
add a comment |Â
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 ifSTY
is populated and forSHLVL
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 for0
is not reliable; you could open two windows and then close window0
, leaving window1
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
add a comment |Â
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 ifSTY
is populated and forSHLVL
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 for0
is not reliable; you could open two windows and then close window0
, leaving window1
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
add a comment |Â
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 ifSTY
is populated and forSHLVL
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 for0
is not reliable; you could open two windows and then close window0
, leaving window1
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
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 ifSTY
is populated and forSHLVL
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 for0
is not reliable; you could open two windows and then close window0
, leaving window1
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
edited Apr 16 at 12:33
Kusalananda
102k13199316
102k13199316
answered Apr 14 at 1:47
Jeff Schaller
31.1k846105
31.1k846105
add a comment |Â
add a comment |Â
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.
This technically quitsbash
but spawns a new interactivebash
shell before it quits, leaving the original shell hanging in itsEXIT
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
add a comment |Â
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.
This technically quitsbash
but spawns a new interactivebash
shell before it quits, leaving the original shell hanging in itsEXIT
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
add a comment |Â
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.
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.
edited Apr 14 at 10:21
answered Apr 14 at 8:18
dotc
163
163
This technically quitsbash
but spawns a new interactivebash
shell before it quits, leaving the original shell hanging in itsEXIT
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
add a comment |Â
This technically quitsbash
but spawns a new interactivebash
shell before it quits, leaving the original shell hanging in itsEXIT
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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