Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Right now I'm running a process within GNU screen. Screen is started with the command screen -dmS screenname and the attached with screen -r screenname. A process is started in the screen session, and is killed after some time.

How would I have the screen terminate when the process inside has ended?

Note: I wouldn't prefer a loop, so I'm looking for an alternative. Sometimes I start the process with an & exit following it, but that never executes if I use kill -9 pid on the process.

share|improve this question

migrated from Jul 2 '12 at 9:22

This question came from our site for professional and enthusiast programmers.

up vote 3 down vote accepted

Start the process with exec, so it will be the only thing inside the screen (it replaces the shell instead of returning to the shell after it's done).

share|improve this answer
Could you give me an example? – hexacyanide Jul 2 '12 at 0:05
After you type screen -r screenname you're put inside the screen at a shell prompt. At that prompt, type exec yourcommand then detach. – Alan Curry Jul 2 '12 at 0:06

Try this:

screen -dmS screenname sleep 5
sleep 3
screen -ls
sleep 3
screen -ls

On the first screen -ls, you should see your screen session. On the second one, it should be gone. Screen will automatically exit when the last window closes. Since you're only running a single program inside screen, the last (and only) window in the screen session will close when the program exits. If you are starting the program by attaching to the screen session and then typing the command at the shell prompt inside the screen session, then screen is waiting for your shell to exit. You can cause this to happen when the program finishes by typing exec PROGRAM instead of just PROGRAM at the shell prompt (actually this will exit the shell and replace it with PROGRAM).

If all you need is the detach ability of screen and nothing else, a lightweight alternative is dtach.

share|improve this answer

You can start the screen session with the name of a command, e.g. screen -dmS session vim will open a screen session that exits with vim. From within an existing screen session, you can use exec to get the same effect:.

share|improve this answer
Just a placeholder I used to test. It'll work the same with anything. – Kevin Jul 2 '12 at 0:10

Your Answer


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.