Difference between revisions of "WorkingOnServers/DisplayEnvironmentTricks"
(→A script for keeping track of screens) |
(→A script for keeping track of screens) |
||
Line 120: | Line 120: | ||
− | + | Save this file in your $HOME/bin directory as 'screenlist' and make it executable: <code>chmod +x screenlist</code>. Then at the command prompt you can type: | |
+ | <pre> | ||
+ | [user@host ~]$screenlist | ||
+ | </pre> | ||
+ | |||
+ | and you should get some output similar to: | ||
+ | <pre> | ||
+ | CURRENT SESSIONS | ||
+ | ------------------------ | ||
+ | 1 3841.coding (09/26/2011 11:35:01 PM) | ||
+ | 2 3791.Ipython (09/26/2011 11:34:42 PM) | ||
+ | 3 17716.test3 (09/26/2011 10:12:15 PM) | ||
+ | ------------------------ | ||
+ | |||
+ | Reattach to session, or ENTER for None: | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | [[User:Jfb|Jfb]] 23:39, 26 September 2011 (CEST) |
Revision as of 23:39, 26 September 2011
A trick for working with SSH, and screen
Often you'll want to ssh to a server, start a job and leave. Knowing that you want the job to stay alive after you log out, you can just use:
nohup myjob.sh > nohup_output.nh
But sometimes you'd rather have an actual term or shell session stay alive. This is often the case when working with ipython. The problem is that your $DISPLAY environment variable may change, so that when you log into the machine later, what screen thinks is the present $DISPLAY variable will no longer be current and you'll get errors.
My workaround for this has been the following:
1 On the host machine, that is the one you are logging into, in your .bash_profile
(which gets read when you log in via ssh) you should add the line:
echo "export DISPLAY=$DISPLAY" > .display.`whoami`.`hostname`
2 Again, on your host machine,make sure the following is in your .bashrc:
## set the prompt command to read the .disply file export PROMPT_COMMAND=". ~/.display.`whoami`.`hostname`"
What is happening here is that everytime you log in, the .bash_profile file creates a new file in your home directory, specific to your user, and the hostname. Whenever you log out and log back in, that file is updated with the appropriate DISPLAY information. In the .bashrc file, we've created a PROMPT_COMMAND that will read that file everytime you hit return in your shell. This can cause some annoyances if the file does not exist, for example if you use the 'su' command to become another user. However, overall it works quite well, such that when you return to a running screen session, the DISPLAY variable will be updated and you'll be able to send X-display windows back to your client machine.
A script for keeping track of screens
Often you may have several screen
sessions running. It is recommended to start screen
with the following command:
screen -S my_description
Where my_description is some string that has meaning to you as a 'session' identifier. If only one screen
session is running then simply screen -r
will reconnect. However, if you have more, things can get complicated. The following script is helpful in such a case:
#!/bin/bash
# filters the screen -ls output to show the sesssions
sessions=`screen -ls | sed -ne 's/[[:space:]]//' -ne 's/\((Attached)\|(Detached)\|(Multi, detached)\|(Multi, attached)\)// p'`
#echo $sessions
#echo $sessions | wc -w
res=`echo "$sessions" | wc -w`
if [[ "$res" != "0" ]]
then
echo ''
echo " CURRENT SESSIONS"
echo " ------------------------"
#screen -ls | sed -ne 's/[[:space:]]//' -ne 's/\((Attached)\|(Detached)\)// p' | cat -n
echo "$sessions" | cat -n
echo " ------------------------"
echo ''
#prompt for the session to join
echo -n " Reattach to session, or ENTER for None: "
read session
if [[ $session != 0 ]]
then
#attach to specified session
linenum=0
name=`screen -ls | sed -ne 's/[[:space:]]//' -ne 's/\((Attached)\|(Detached)\|(Multi, detached)\|(Multi, attached)\)// p' |
while read line
do
let "linenum += 1"
if [[ "$linenum" -eq "$session" ]]
then
echo $line
break
fi
done`
shrtname=`echo $name | sed -e 's/[[:space:]]//' -e 's/(.*)//' -e 's/\t//g'`
echo "Reattaching to: " $shrtname
if [[ "$name" != "" ]]; then
if
#echo $shrtname
screen -r "$shrtname"
then
echo -n " Bye bye screen.. Exit?[y] "
read nbye
if [[ "$nbye" == "" ]]
then
exit
else
goodbye=$nbye
fi
if [[ "$goodbye" == "y" ]]
then
exit
else
echo "What's next?"
fi
else
if
# try multiuser connect
screen -Ax "$shrtname"
then
echo "Hope that was fun!"
else
echo "Trying to Force connection"
screen -D -r "$shrtname"
fi
fi
else
echo "Could not reattach to $session"
fi
else
echo " Have fun..."
fi
else
echo " No existing SCREEN session to reattach to..."
fi
Save this file in your $HOME/bin directory as 'screenlist' and make it executable: chmod +x screenlist
. Then at the command prompt you can type:
[user@host ~]$screenlist
and you should get some output similar to:
CURRENT SESSIONS ------------------------ 1 3841.coding (09/26/2011 11:35:01 PM) 2 3791.Ipython (09/26/2011 11:34:42 PM) 3 17716.test3 (09/26/2011 10:12:15 PM) ------------------------ Reattach to session, or ENTER for None:
Jfb 23:39, 26 September 2011 (CEST)