Link to home
Start Free TrialLog in
Avatar of Drizzt95
Drizzt95

asked on

Run TOP from crontab

I don't know too much about unix, but am wanting to find a way to regularly have the ouput of top sent to a file.  I run a line which works perfectly, but when I add it to a cron job it always fails.  The line I am running is:

top -d1 > /rhc/nt1/top.txt

It works when I run it, but it won't work from a cron job.  There is a couple of processes that we need to keep an eye on and if they get in the top 10 then i need to be notified.  If i can't do it with top, is there any other way to get the top 10 processes.
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
-d 1 does not work in cron (check your mails send by crond:)
ahoffmann, if u ran it via a shellscript, will it not work then?
no, 'cause top requres a tty for most options, and with cron there is no tty
it does not make a difference if there is a (useless) shell around top
ok, thanks for that
I tried running via shellscript, it does seem to work (well it work cos of tty - now that I know), but I have not tried it with cron
Reason why I suggested a shell script is so that more can be done, instead of running just top.
For example, you may want to email the results or whatever.
Its also useful for validation ie. directory specified still exists or is not writable, easier to log errors
Avatar of Drizzt95
Drizzt95

ASKER

I did originally have it running from a script, but that didn't work either.  It ran fine when i execute it myself, but when it runs via a cron job, it just creates an empty file everytime.
Drizzt95, does ahoffman's idea work?


question for ahoffman

if term was set to xterm, in the shell script and top run then, would that make a difference?

TERM=xterm
export TERM

> if term was set to xterm, in the shell script and top run then, would that make a difference?
no
the reason is the tty, which is a device, not the term(inal)
ok., thanks ahoffman
ahoffman's idea didn't work either, just created a blank file.

If i try running the crontab entry from unix, I get the following error:
/usr/bin/top: syntax error at line 1 : `(' unexpected

The command I was running is:
 cron_env /usr/bin/top -d1 > /rhc/nt1/top.txt

The cron entry I have is:
0,5,10,15,20,25,30,35,40,45,50,55 6-20 * * * cron_env /usr/bin/top -d1 > /rhc/nt1/top.txt

Like i said, I don't know too much about unix, but every other entry in crontab uses the cron_env.  I've tried replacing it with "su sys -c "/usr/bin/top -d1 > /rhc/nt1/top.txt", which also creates a blank file, but doesn't cause any errors.
I managed to get it working.  The statement below seems to work from a cron job, for some reason the " > /rhc/nt1/top.txt" part was causing the problem.

top -d1 -f /rhc/nt1/top.txt
> If i try running the crontab entry from unix, I get the following error:
> /usr/bin/top: syntax error at line 1 : `(' unexpected

this could never be from the command I suggested (except *your* /usr/bin/top is a shell script already)
It only gets that error when I run it with the cron_env in front of it, which all of our cron jobs have.  If i run it without the cron_env then it also fails in a cron job (works fine when run manually):

cron_env /usr/bin/top n 1 > /rhc/nt1/toptest.txt
/usr/bin/top: syntax error at line 1 : `(' unexpected
what does cron_env do?
is /usr/bin/top a script?
absolutely no idea, it was set up before I started here.  I've just continued to do as what was done in the past.

/usr/bin/top is an executable.

I'm not sure if this would make any difference, but I did just notice that there is an alias set up for top.  It is: alias -x top='top -s1 -n10 -q'
I'm not sure if that would affect it at all.

To be honest, I'm not too worried, we'll only have this server for another couple of months and I've managed to get it working, which is all I wanted.

Seeing as though you both showed me a bit, I'll split the points evenly between you.

Thanks for your help.
no worries. Glad you got going though
> but I did just notice that there is an alias set up for top
that's why I always used /usr/bin/top in all my posts ;-)