Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What's the best way to run a command inside a screen session such that its parent shell can be accessed? I want to be able to restart it from within the same screen session.

The best I've managed to come up with is the following:

$ cat mr-t.sh 
#!/bin/bash

top
exec /bin/bash

and then:

screen -e'^\\\' -S top-in-screen ./mr-t.sh

Then, if top stops running, I'll at least get a new shell within the same screen session I can work with. This isn't very elegant, though, and I can't quite convince myself that the signal will reliably be sent to the right process if I hit C-c. Moreover, C-z doesn't work at all.

I'm using bash 3.2.48 on OSX and 3.2.39 on Linux. Both are probably patched by OS vendors.

There's nothing special about the top command here, of course.

[As an aside, -e'^\\\' reassigns the Magick Screen Key from C-a (a bad default if there ever was one) to C-\.]

share|improve this question
    
What is wrong with just starting top from the shell? –  vonbrand Feb 10 '13 at 1:58
    
@vonbrand Is that a serious question? Do you really think I'm running top? More to the point, can you really think of no use cases for wanting to automate starting a long-running program in a manner that makes debugging problems easy? –  Michael Wolf Jan 8 at 20:54
add comment

2 Answers

Create a ~/.screenrc.top like so:

screen
stuff "top\015"

Now run screen -c ~/.screenrc.top. No race condition!

share|improve this answer
add comment

Here's an imperfect solution I found.

screen -e'^\\\' -dmS top-in-screen

Then you can run:

screen -S top-in-screen -p 0 -X stuff 'top^M'

(Note that although you'll only see ^M you should actually type C-v followed immediately by C-m.)

It's imperfect because (I think) there is a race condition between the first and second invocations of screen.

From http://aperiodic.net/screen/faq#how_to_send_a_command_to_a_window_in_a_running_screen_session_from_the_commandline, which has lots of other good information as well.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.