You probably familiar with manipulating screen operations in your screen sessions. How about “outside” the screen session? Here are some tips about it. If you’re interested, please have a look.
Manipulate Screen from Outer Space
Create screen session with a window running specified command:
$ screen -dmS testing -t shell bash
Turn on log:
$ screen -S testing -pshell -X logfile "/tmp/screen-pshell.log" $ screen -S testing -pshell -X log on
$ screen -S testing -pshell -X stuff 'ping 126.96.36.199\015'
Take a peek at screen window:
$ screen -S testing -pshell -X hardcopy $(tty)
Create new window in the screen session:
$ screen -S testing -X screen -t monitoring bash
Run command in previously created window:
$ screen -S testing -pmonitoring -X stuff 'htop\015'
Terminate foreground running process through keyboard interrupt:
$ screen -S testing -pshell -X stuff '\003'
Kill specific window of the screen:
$ screen -S testing -pshell -X kill
Quit entire screen Session:
$ screen -S testing -X quit
Okay, the aforementioned skills are useful enough. But why should I use them? Under what circumstances should I create a new window inside specific screen session? And I’ll provide an example below.
A More Complex Application
Suppose I want to run a command in foreground, say
ping, and keep track of
its pid if it is running. Otherwise the failure message should be logged.
Though this scenario is a bit trivial, it help us get to understand how to
assemble the screen skills learnt above.
ping will success, and its pid is in
ping.pid. The service
continues running on foreground:
$ screen -S testing -pshell -X stuff 'ping 188.8.131.52 & echo $! > ping.pid; fg || echo "ping failed to start" | tee "ping.failure"\015'
ping will fail, and the error message will be written in
$ screen -S testing -pshell -X stuff 'ping 8.8.8. & echo $! > ping.pid; fg || echo "ping failed to start" | tee "ping.failure"\015'