0

I have this cronjob: (it's a single line, I formatted it only here for better readability)

* 5-7 * * * ( 
   sudo pstree -pa > "$HOME/log/`date "+\%F-\%T"`-pstree.txt";
   sudo top -o \%MEM | head -25 > "$HOME/log/`date "+\%F-\%T"`-topMEM.txt";
   sudo top -o \%CPU | head -25 > "$HOME/log/`date "+\%F-\%T"`-topCPU.txt"
) 

The first command runs successfully, the file is created with pstree inside it.

But the second and third commands fail and result in empty files.

The only info in the the mail is:

'unknown': unknown terminal type.

I'm just starting out with Debian, etc, so sorry for the newbie question, any advice is welcome! :)

Tudvari
  • 2,715
  • 2
  • 14
  • 33
  • Does [this](https://stackoverflow.com/questions/24676687/top-xterm-unknown-terminal-type/28290724) answer your question? – tink Sep 15 '21 at 09:49
  • 1
    `top` is intended for interactive use on a terminal. When run from `cron`, its `stdout` is not connected to a terminal. Maybe `top`'s `-b` (Batch-mode) and `-n` (Number-of-iterations) options would help. see [man top](https://man7.org/linux/man-pages/man1/top.1.html) – Bodo Sep 15 '21 at 10:46
  • 1
    use `top` with `b` if you want to send result to a file. Else top will update screen (and so it need a terminal, and so you cannot redirect the result). – Giacomo Catenazzi Sep 15 '21 at 11:34
  • Thanks, adding the b flag solved it, thanks! – Tudvari Sep 15 '21 at 15:24

1 Answers1

0

top's -b (Batch-mode) and -n (Number-of-iterations) options helped.

By default top is interactive so I have fetch a static result to save it into a file.

Example: top -b -n1

Tudvari
  • 2,715
  • 2
  • 14
  • 33