Search code examples
linuxstdout

Catpture output of Linux command `needrestart`


I'm trying to capture the output of Linux command needrestart, but I'm having no luck...

After intensive googling, I would expect the following to work:

root@NASLG2:/home/erik# needrestart 2> /home/erik/needrestartoutput.txt

But /home/erik/needrestartoutput.txt remains empty...

Any thoughts?

(My goal is to check whether the output of needrestart contains the substring Service restarts being deferred:.)

Edit: The following also has no effect on needrerstartoutput.txt:

root@NASLG2:/home/erik# needrestart > /home/erik/needrestartoutput.txt

Solution:

needrestart -v > /home/erik/needrestartoutput.txt

Some more insight:

I was actually on my way to create this:

#!/bin/bash
NEEDRESTART=$(needrestart -v)
STRINGTOSEARCH="Service restarts being deferred"

if [[ "$NEEDRESTART" =~ "$STRINGTOSEARCH" ]]
then
        reboot
fi

The above question rose because I wanted to see the output in a file, since I was unable to get it as a variable. But the file itself I do not need.


Solution

  • Apparently needrestart doesn't produce output by default when its output is redirected. Add the -v option for verbose output.

    needrestart -v > filename