Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Run TOP from crontab

Posted on 2006-05-22
19
Medium Priority
?
560 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:Drizzt95
  • 7
  • 7
  • 5
19 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 16740250
/usr/bin/top n 1 > /rhc/nt1/top.txt
0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 500 total points
ID: 16740274
try ahoffmann's idea, that should work

alternative is to place your working code inside a shellscript e.g. run_top
then call run_top in your cronjob


With your shellscript, it is now a good idea to get to grips with using variables. Try to avoid hardcoding
e.g.

OUTFILE="/rhc/nt1/top.txt"

# this writes to the file, overwrites file it if already exists
top -d1 > $OUTFILE

# This appends
top -d1 >> $OUTFILE

What do u do with the file, once its created?
U could start logging on your append

date >> $OUTFILE
top -d1 >> $OUTFILE

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16740842
-d 1 does not work in cron (check your mails send by crond:)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 65

Expert Comment

by:rockiroads
ID: 16740866
ahoffmann, if u ran it via a shellscript, will it not work then?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16740989
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
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16741228
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
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16741246
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
0
 
LVL 3

Author Comment

by:Drizzt95
ID: 16741834
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.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16742243
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

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16742345
> 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)
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16746127
ok., thanks ahoffman
0
 
LVL 3

Author Comment

by:Drizzt95
ID: 16747168
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.
0
 
LVL 3

Author Comment

by:Drizzt95
ID: 16748351
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16748725
> 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)
0
 
LVL 3

Author Comment

by:Drizzt95
ID: 16748866
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16749124
what does cron_env do?
is /usr/bin/top a script?
0
 
LVL 3

Author Comment

by:Drizzt95
ID: 16756556
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.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16758151
no worries. Glad you got going though
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16758594
> 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 ;-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question