User avatar
RDK
Posts: 244
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Screen Application

Mon Sep 24, 2018 4:02 pm

I use the terminal multiplexer "screen" application a lot. I start a screen session in my rc.local at boot-up like this:

Code: Select all

su xxx -c "screen -d -m /usr/bin/python /mnt/usbdrive/pgms/mypythonpgm.py"
where xxx is the user who will own the session.

If I'm at a command prompt I start a screen session like this:

Code: Select all

screen bash
, note no "sudo".

Today I realized I really don't know what the "bash" options does. I've looked at the MAN pages and did not see anything which jumped out at me. Can someone enlighten me?

Also, operationally I've noticed that for the screen session started by the rc.local, if I terminate the running program (mypythonpgm.py) or it ends itself that the screen session automatically closes. On the other hand, if I have created the screen session manually, ie

Code: Select all

screen bash
then I can start a python program and the terminate it without an automatic session closure. Why?

I actually would normally prefer the latter case where the screen session does not close automatically when the program ends, for example with some kind of unplanned error.

Thanks for your ideas/thoughts.....RDK

User avatar
rpdom
Posts: 14417
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Screen Application

Mon Sep 24, 2018 5:34 pm

bash is the command shell. It provides the cli environment for you so you can type commands and have them executed. When you are at a command line prompt you are (usually) running in bash. Try 'man bash'

tpyo kingg
Posts: 549
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Screen Application

Mon Sep 24, 2018 6:11 pm

bash is one of the scriptable user interfaces (UI) you can use interactively. zsh is another, as are ksh, dash, and many others. When run in the interactive mode, it functions as a UI between you and the operating system. The interactive mode won't end until you tell it to, say by typing 'exit'. However once you do quit the shell, screen will end just like when your python script ends. If you don't quit, bash will keep running and thus screen will too.

You can look at 'zombie' and 'defzombie' in the screen manual page for some flexibility in that regard. I usually prefer tmux instead of screen and it has a 'remain-on-exit' setting if it is necessary to keep a session open past the completion of the process being run.

tldr; screen won't quit until the program it is running quits

If you would like background information and you have the patience for videos, the first half of this one covers a lot of material related to how the various shells have developed over time and are developing:

https://www.youtube.com/watch?v=v1m-WEZz46U

User avatar
RDK
Posts: 244
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Screen Application

Tue Sep 25, 2018 5:39 am

Thanks for the information, I've started looking into tmux.
However, in the mean time, is there an option or keyword that I can add to my rc.local command

Code: Select all

su xxx -c "screen -d -m /usr/bin/python /mnt/usbdrive/pgms/mypythonpgm.py"
so that this session does not end if the program itself terminates? I've gotten recently a couple unexpected terminations which, hopefully, seeing the error messages when it ends might be helpful,
Is "bash" the default? I don't seen any operational differences, except for the introduction disclaimer text, between just "screen" and "screen bash"....RDK

tpyo kingg
Posts: 549
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Screen Application

Tue Sep 25, 2018 7:28 am

It's not well documented but you could put the following in .screenrc

Code: Select all

zombie kr
Then when you re-attach to the screen you will be asked if you wish to kill the window by pressing 'k' or restart the process that failed by pressing 'r'

In tmux you could launch a new session labelled "foobar" like this:

Code: Select all

tmux new-session -s foobar -d /usr/bin/python /mnt/usbdrive/pgms/mypythonpgm.py \; set remain-on-exit on \; 
and re-attach like this

Code: Select all

tmux attach -t foobar
and when it is stuck after the main process has died, kill it from outside tmux like this

Code: Select all

tmux kill-pane -t foobar
Or from inside by ctrl-b :kill-pane

User avatar
RDK
Posts: 244
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Screen Application

Tue Sep 25, 2018 1:13 pm

Perhaps I need a bit of clarification?
Then when you re-attach to the screen you will be asked if you wish to kill the window by pressing 'k' or restart the process that failed by pressing 'r'
When you say "restart the process that failed", I'm not sure if "process" means the screen session window or the program that was started when I defined the screen session in my rc.local. Can you expand a bit on this?

I'm experimenting with tmux, but I will stick to screen, until I'm comfortable that tmux will work the way I want it to work.

Thanks...RDK

tpyo kingg
Posts: 549
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Screen Application

Tue Sep 25, 2018 1:23 pm

The restart in that context refers to the program running inside screen, such as your python script.

User avatar
RDK
Posts: 244
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Screen Application

Tue Sep 25, 2018 6:51 pm

Hmmmm, that was what I suspected. I don't really what to restart that program/script, I really want to see the error messages which should be displayed when the program/script terminates. Oh well....RDK

User avatar
HawaiianPi
Posts: 4194
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Screen Application

Tue Sep 25, 2018 9:31 pm

I control my headless Minecraft SMP servers via SSH, and launch them using screen. By using the keys Control+D followed by A you can exit a screen session without terminating the session or the program you have running in the session. That allows me to close the SSH connection without killing the server, and I can later SSH in and reconnect to the server with screen -r session_name.
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

tpyo kingg
Posts: 549
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Screen Application

Wed Sep 26, 2018 4:28 am

RDK wrote:
Tue Sep 25, 2018 6:51 pm
[...] I really want to see the error messages which should be displayed when the program/script terminates.
Then 'zombie' setting in screen or 'remain-on-exit' setting in tmux will keep the session and its output around for inspection even after the program that was running terminates. As HawaiianPi points out, you can reconnect to examine the leftovers using "screen -r" or I'd add "screen -rd". In tmux that would be "tmux a" or "tmux a -d"

User avatar
RDK
Posts: 244
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Screen Application

Wed Sep 26, 2018 5:15 am

Thanks folks....I'm resurrecting an old PiB+ Jessie to experiment with all of these ideas. I'm aware of the "screen -r" command, but the screen session created by my rc.local itself is closed when my Python program ends, so there is no screen session to attach to.

Rebuild in progress and experiments to follow. Thanks again....RDK

User avatar
RDK
Posts: 244
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Screen Application

Wed Oct 10, 2018 5:30 pm

Folks.....I have finally gotten back to this project. I have installed tmux and am playing with it.

However, I find it had the same problem as "screen" when I start it with this command:

Code: Select all

 tmux new-session -s foobar -d "/usr/bin/python /mnt/usbdrive/pgms/testa.py"
The test program run (counts down from 30 and then ends normally) and when it finishes the tmux window closes. I really want that window to stay open (or re-attachable) so I can look at the program output.

I have looked at the man page and do not easily see an option to keep the window open/active. Am I missing something?...RDK

tpyo kingg
Posts: 549
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Screen Application

Wed Oct 10, 2018 5:55 pm

Try the example given earlier where set remain-on-exit is used.

User avatar
RDK
Posts: 244
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Screen Application

Thu Oct 11, 2018 8:07 am

tpyo king....OK, I looked at that possibility but am not sure how to implement it. As I'm trying to do this via a command in the rc.local, it is not clear where and how to set this option for just the window being created by the rc.local.

I was searching the web and found this possible answer which I'm now testing from the CLI.

Code: Select all

tmux new-session -s foobar -d "/usr/bin/python /mnt/usbdrive/pgms/testa.py; exec bash"
Your thoughts?.....RDK

tpyo kingg
Posts: 549
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Screen Application

Thu Oct 11, 2018 8:18 am

Perhaps this gets you closer:

Code: Select all

tmux new-session -s foobar -d "/usr/bin/python /mnt/usbdrive/pgms/testa.py" \; set remain-on-exit on \; 
The backspaces are needed so that the semicolons go to tmux rather than getting interpreted by the shell. And "foobar" is the name of the session. Probably a more descriptive name would help.

Then you can attach later and see if the process is dead or alive:

Code: Select all

tmux ls
tmux attach -t foobar
If it is dead, you can either close the session from another terminal:

Code: Select all

tmux ls
tmux kill-pane -t foobar
tmux ls
or else while still inside the dead pane: ctrl-b:kill-pane
Note that there is a colon after the ^b and before the tmux command.

User avatar
Cancelor
Posts: 756
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Screen Application

Thu Oct 11, 2018 8:33 am

RDK wrote:
Wed Oct 10, 2018 5:30 pm

The test program run (counts down from 30 and then ends normally) and when it finishes the tmux window closes. I really want that window to stay open (or re-attachable) so I can look at the program output.
Are you sure that it is not re-attachable? The test program has ended and your connection to tmux or screen is lost but it may still be re-attachable.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
Cancelor
Posts: 756
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Screen Application

Thu Oct 11, 2018 8:36 am

tpyo kingg wrote:
Tue Sep 25, 2018 7:28 am
It's not well documented but you could put the following in .screenrc

Code: Select all

zombie kr
Then when you re-attach to the screen you will be asked if you wish to kill the window by pressing 'k' or restart the process that failed by pressing 'r'
This looks promising.

Have a look at https://www.gnu.org/software/screen/man ... Files.html and add the zombie kr line to that file.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
RDK
Posts: 244
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Screen Application

Thu Oct 11, 2018 8:58 am

tpyo king.....OH, now I understand, I mistakenly thought the

Code: Select all

 \; set remain-on-exit on \;
were comments. Sorry for the confusion....RDK

tpyo kingg
Posts: 549
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Screen Application

Thu Oct 11, 2018 9:18 am

Understandable. They kind of look like that at first glance, for future reference comments in the shell are the pound sign #.

Aso for the semicolons, unescaped ; they are a line separator, escaped \; they are passed literally from the shel (UI) to the program.

Code: Select all

echo foo \; date \; # these semicolons are interpreted literally

echo foo ; date; # these semicolons are interpreted by the shell

echo foo \; date \; \# these semicolons and pound sign are interpreted literally

Return to “General discussion”