screen to detach from workWithin an SSH connection, if you start an interactive program it is likely to stop working when you close your terminal. You can set up your VM so that the program will keep working after you close your connection. You can use a program called screen for that.
images and templates that you have created so far; but you can always import a new appliance from the AppMarket).example.sh. You can use your favourite editor. In our example we use nano, like this:cd
nano example.sh
#!/bin/bash
while sleep 2
do
date
done
Save the file and exit the editor. For example, in nano, you press control-x and then you press Y to confirm that you want to save your changes. Then you are back in the terminal.
Make the script executable:
chmod u+x example.sh
./example.sh
control-c./example.sh
ps or pstree that your script runs,pstree
# look for a line like this:
# ... -example.sh---sleep
Food for brain:
Is
pstreecommand installed? If not, are you able to figure out how to install it? After all, you are the sysadmin!Suggestion just in case:
- Find the Linux distribution (aka distro) name and version you are running.
- Find the package for the distro you are running that provides
pstree.- Install the package using the package manager for your linux distro. Tip, try the
aptcommand-line tool.
ps or pstree again to verify the the execution has stopped.nohupnohup to run the example script in the background:nohup ./example.sh &
The output will be:
nohup: ignoring input and appending output to ‘nohup.out’
ps or pstree and have a look at the progress:tail -f nohup.out
ps or pstree to verify that your script is still runningtail -f nohup.out
killall example.sh
For more information read the manual, it is small and simple but effective.
man nohup
screenYou will use a program called screen. Similar programs are: tmux, dtach+dtvm.
screen
Enter keypstree to see that you are actually in a screen session: look for a line similar to├─sshd───sshd───sshd───bash───screen───screen───bash───pstree
./example.sh
control-a, then dscreen and line says something similar to:[detached from 14691.pts-0.145]
-r flag:screen -r
control-a plus d)find the script still running
control-c)control-d)[screen is terminating]screen -rscreen sessionsscreen command has a lot of options. Reading the manual page will provide you with a wealth of information.
Examples:
screen, type a command, so that you can recognize the sessioncontrol-a c (lowercase C)control-a control-a (yes, twice)control-a 1 (digit one)control-a 0 (digit zero)control-a c, as before)Play with these switching commands between the three sessions using control-a and a digit or the control-a control-a sequences
control-a w (lowercase W)screen, type control-a S (capital S)control-a TABcontrol-a TAB) and between sessions in that pane