Re: Something Odd - "top" Delivers Nada When Run By Crontab

c186282 <[email protected]>
Newsgroups comp.os.linux.misc
Organization wokiesux
Message-ID <[email protected]>
On 7/11/26 00:34, Rich wrote:
> c186282 <[email protected]> wrote:
>> Wrote a short Python script that uses os.system() to run 'top' ...
>> redirecting the output to a file.  We then clean-up the weird chars
>> in the file and append it to an ongoing log file.
>>
>> Run the script from the CL and it works perfectly.
>>
>> Run it via root crontab though and the top redirect file exists, but
>> is EMPTY.
>>
>> ...
>>
>> The line :
>>    os.system("top -n 1 > topp.txt")
>> We then clean up weird chars and append it to the real log file.  DID
>> check if there was any output BEFORE the clean-ups.  Nada.
>>
>> I've tried firming up the path - but I do an os.chdir() anyway using
>> sys.argv[0] at the top of the script anyhow.  Regardless, the 'top'
>> output delivers NOTHING to the dest file.  DOES create it, but ...
>>
>> So ... why doesn't this work ???
> 
> Very likely because you overlooked the "batch" CLI argument:
> 
>      -b  :Batch-mode operation
>          Starts top in Batch mode, which could be useful for sending
>          output from top to other programs or to a file.  In this mode,
>          top will not accept input and runs until the iterations limit
>          you've set with the `-n' command-line option or until killed.


   I'll try it ! :-)

   Still weird the CL -vs- Crontab behavior is SO different
   for such an old basic utility.


>> 'top' is 'ubiquitous', nothing new there.
> 
> Top also expects (in normal operation mode) to be connected to a tty so
> it can receive input from a user.  When run from a crontab there is no
> tty, and top likely aborts when it finds that it's file descriptor zero
> is not a tty.  The batch mode argument is described in the man page in
> such a way that it implies it turns off the "check that fd[0] is a tty"
> test.
> 
>> The different behavior from the CL vs crontab is puzzling.  Been
>> screwing with this all day, trying lots of little variants.
> 
> It is not at all puzzling for anyone who understands the difference
> between programs that expect tty's and those that don't.

   I do know there's a diff - but didn't expect it HERE.

   BTW, tried both a conventional crontab entry AND one
   with a "&" at the end - which usually launches an
   independent process.

   'Top' is USUALLY used as something that's constantly
   running kinda like 'ping'. However the "-n" param lets
   it do just ONE scan and then exit. Yea, there are other,
   really grungy, ways to get similar info, but why re-invent
   what's basically already there ???
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.