Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

This question already has an answer here:

I have a server with a hard drive I'd like to test using smartctl (smartmontools) and badblocks. I would use smartctl first, then badblocks.

My question is three parts:

  1. How can I run the commands over SSH so the process won't stop when I close the Terminal?
  2. How can I output the badblocks results and the smartctl info to a file?
  3. How can I run smartctl and then badblocks will start automatically (when smartctl is finished) without having to SSH again?

Thank you.

share|improve this question

marked as duplicate by Scott, Tog, Kevin Panko, harrymc, Simon Sheehan Jun 18 '14 at 20:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Although I detect a serious lack of research effort, here’s an answer.

  1. nohup wraps a process so that it doesn’t receive the hangup (HUP) signal. As such, it will continue running after the session is closed.

    For more sophisticated features, you could use screen or tmux, which work much like a disconnected remote desktop session on Windows. Programs keep running and the output is collected. It will be presented to you when you reconnect to the screen/tmux session.

  2. Output redirection is the keyword here, with the corresponding operator being > as in echo bla > file.txt.

  3. You don’t. That’s because it’s not actually smartctl doing the test, but the hard drive itself. There is no notification when it’s finished, you have to poll for status/results periodically.

share|improve this answer
Wouldn’t (smartctl && badblocks) & answer question 3? (Or at least nohup sh -c 'smartctl && badblocks' &, or running a script that does smartctl && badblocks with nohup and &?) – Scott Jun 17 '14 at 20:37
No, it doesn’t. He wants to run a SMART self-test on the hard drive. This test is started by smartctl, which exits immediately. The test is executed by the drive itself. Its status can be polled. – Daniel B Jun 18 '14 at 21:36

The way to do it is to log in via SSH, and then start screen.

Within screen, run the commands like this:

 smartctl <options>; badblocks <options>

Now, type Ctrl+AD to "detach" from the screen session. That way it will keep running when you close SSH.

You can "reattach" the screen session when you log in again later by running screen -r.

If you still want to save the command's output to a file, run them like this:

(smartctl <options>; badblocks <options>) >/path/to/log/file.txt
share|improve this answer

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