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 ???
'top' is 'ubiquitous', nothing new there.
The different behavior from the CL vs crontab is puzzling. Been
screwing with this all day, trying lots of little variants.
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.
'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.
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.
When I run `env -i top` I get _no_ output at all.
In comp.os.linux.misc, c186282 <[email protected]> wrote:
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 environment variables for cron jobs are usually nearly empty.
Anytime something works from the shell but not from cron, check
environment variables.
Here's a tip: your cron scripts should work with _no_ environment
variables at all. To run them that with, use `env -i`. When I run
`env -i top` I get _no_ output at all.
The "-b" option suggested in another post fixes the no output problem.
Elijah
------
top probably wants $TERM set when not in batch mode
On Sat, 11 Jul 2026 05:46:23 -0000 (UTC), Eli the Bearded wrote:
When I run `env -i top` I get _no_ output at all.
It needs the TERM environment variable to be defined:
env -i TERM=xterm top
produces a top display.
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.
On Fri, 10 Jul 2026 23:57:54 -0400, c186282 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.
I think we've been here before...
https://docs.python.org/3/library/subprocess.html
Yes, it's one of those hateful, newfangled functions that might work for
you.
On 7/11/26 02:40, rbowman wrote:
On Fri, 10 Jul 2026 23:57:54 -0400, c186282 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.
I think we've been here before...
https://docs.python.org/3/library/subprocess.html
Yes, it's one of those hateful, newfangled functions that might work
for you.
Um ... there are LOTS of 'monitoring' apps for Linux at this point -
just did a survey a couple of days ago. Most are serious OVER-kill.
They also tend to record SO much shit that you'll never be able to
find YOUR particular problem in the mess.
On 7/11/26 02:40, rbowman wrote:
On Fri, 10 Jul 2026 23:57:54 -0400, c186282 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.
I think we've been here before...
https://docs.python.org/3/library/subprocess.html
Yes, it's one of those hateful, newfangled functions that might work for
you.
Um ... there are LOTS of 'monitoring' apps for Linux
at this point - just did a survey a couple of days
ago. Most are serious OVER-kill. They also tend to
record SO much shit that you'll never be able to find
YOUR particular problem in the mess.
Anyway, the "-b" param to 'top' did do the trick.
Still not sure why evoking from the CL is so different
from evoking via crontab - but that's how it is.
It needs the TERM environment variable to be defined:
env -i TERM=xterm top
produces a top display.
| Sysop: | DaiTengu |
|---|---|
| Location: | Appleton, WI |
| Users: | 1,128 |
| Nodes: | 10 (0 / 10) |
| Uptime: | 51:54:30 |
| Calls: | 14,453 |
| Files: | 186,423 |
| D/L today: |
87 files (29,666K bytes) |
| Messages: | 2,558,431 |