I want to make a shell script which copies the past outputs of the GUI terminal emulator (for example, last 20 lines). The motivation is as following:

When I execute a procedure which requires long time (for example, downloading a very large file, or converting a very large movie file), I sometimes remember another job, and I have to leave the room. In such a case, I press ctrl+z to stop the procedure. And I type

fg; echo $? >> log.txt; date >> log.txt; systemctl poweroff

then I leave the room.

This way works and is not bad. But it has a disadvantage that I cannot read the outputs of the procedure. I can know only the status ($?). So I want to copy last 20 or 40 lines and save them in log file.

  • 1
    If you do this a lot, you'd be better off getting into the habit of redirecting the output to a file (or using tee to redirect to a file AND to stdout). Alternatively, depending on which window manager you use, you could use a screenshotting tool - most allow you to screenshot a particular window rather than the entire screen. e.g. xfce4-screenshooter's -w option takes a shot of the currently active window (if necessary, you could use wmctrl or similar to select which window is active). Finally tmux has a save-buffer option to save the current buffer to a file.
    – cas
    Apr 11, 2022 at 5:55
  • 1
    and one more option: just don't bother powering it off at all - is that essential just because you've left the room? You could just lock the screen or suspend/hibernate the machine instead of powering it off.
    – cas
    Apr 11, 2022 at 5:59
  • 1
  • @cas Thank you for your comment. Screenshot is easy and useful. And you are right, power off is not essential. I want to stop electricity, so hibernation seems to be better for my purpose.
    – user356126
    Apr 14, 2022 at 1:25

1 Answer 1


Run your command with nohup, screen or tmux in the first place. Of course this won't help if you already started your process.

If that is the case, you can capture output of your command using strace:

strace -p<PID> -s9999 -e write  2>&1 | grep -o '".\+[^"]"'

(replace <PID> with the PID of your process)

If strace cannot attach to the process, you might need to run as root / with sudo or change your ptrace settings to 0 (and be aware of the security implications of it!):

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

You can redirect that output to a file then.

There are other options, e.g. gdb or reredirect. See here or here.

  • I just tested in Ubuntu, and it says it cannot attach. "If your uid matches the uid of the target process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try again as the root user." With sudo it works fine. That file has content 1 for me.
    – pLumo
    Apr 11, 2022 at 7:08
  • 1
    added information. Thanks!
    – pLumo
    Apr 11, 2022 at 7:13
  • Thank you for your answer. strace can save all the outputs, so it will help me a lot. I have used strace sereral times, but I couldn't think of using strace for such a purpose.
    – user356126
    Apr 14, 2022 at 1:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .