LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-17-2011, 08:08 AM   #1
Gr1f1S
LQ Newbie
 
Registered: May 2011
Posts: 2

Rep: Reputation: 0
No output from top command run by crontab


Hello ,
I write a little script that run top command and clear the output leaving only cpu ram and swap values.
If i run the script manually everityng works fine but when i schedule the cript to run every 5 minutes from /etc/crontab all run fine but the output of the top command doesnt appear in the log :

#############################

mar mag 17 14:30:01 CEST 2011

#############################



This is the cript :

#!/bin/sh
echo "#############################" >> /var/log/performance.log
echo "" >> /var/log/performance.log
/bin/date >> /var/log/performance.log
/usr/bin/top -b -n 1 | grep -E "Cpu|Mem|Swap" >> /var/log/performance.log
echo "" >> /var/log/performance.log
echo "#############################" >> /var/log/performance.log


OS Type Linux debian 3.1 .
Any ideas?
 
Old 05-17-2011, 09:55 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,053
Blog Entries: 14

Rep: Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226
You put the full path to top in the script but not the full path to grep.

When you run from command line the script inherits your environment including PATH so knows where to find binaries. When you run it from cron it has a minimal environment. You can solve that by any of the following methods:
1) Set the full path for each command in the command line. As noted above you did that for top but not for grep.
2) Set variables equal to the full path then use the variables instead of the command:
e.g.
Code:
export TOP=/usr/bin/top
export GREP=/bin/grep
...
$TOP -b -n 1 | $GREP -E "Cpu|Mem|Swap"
...
3) Explicitly set the PATH variable before calling any other commands:
Code:
export PATH=/bin:/usr/bin
...
Also verify "/bin/sh" in your first line is invoking the shell you intend. On many systems it is a link to bash but I've seen others where it is linked to something like dash.
 
Old 05-17-2011, 10:02 AM   #3
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
Everything else seems to be printing out fine, so obviously the problem is in the command string with the top command.

You're following crontab best-practices by specifying the fully-qualified path of all your commands, except one: grep. Try adding the path to that one and see what happens.
 
Old 05-17-2011, 10:48 AM   #4
Gr1f1S
LQ Newbie
 
Registered: May 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Hello ,
Thanks to all for your help.
I try both the way you suggest me but nothing as changed.

I put the full path at the grep command as suggested by SL00b with the same result everityng print fine but no top output recorded , so i try to remove the path and declare the path variables as suggested by MensaWater same result

thanks
 
Old 05-17-2011, 11:00 AM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,053
Blog Entries: 14

Rep: Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226Reputation: 1226
Actually I suggested 3 different ways including the one Sloob later suggested.

Your script works fine for me on CentOS5 from cron so I'm not sure what the issue is on your Debian system.

I notice you didn't mention what shell /bin/sh is pointing to - do an "ls -l /bin/sh" and let us know.

My first thought on seeing your post was that it might not like the fact you don't have a terminal as top normally runs in a terminal but nothing in my CentOS5 man page for top talks about that. You might want to review the man page (man top) to see if it has any indication that your version of top has such a restriction.

When you put this in cron what user's cron are you using?
 
Old 04-23-2013, 06:47 AM   #6
eng_michael_magdy
LQ Newbie
 
Registered: Apr 2013
Posts: 1

Rep: Reputation: Disabled
Smile Many Thanks :))))

Writing the full path in the crontab for both (top and grep) worked perfectly for me
Thank YOU !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
my database export shell script run from command line but not form crontab saifee Linux - General 11 01-29-2012 10:24 AM
About the top command output jacobselvin Linux - General 1 07-17-2009 01:16 AM
Output of a C program which contains Linux's TOP command trups8 Programming 8 02-07-2009 07:14 AM
problem with output of ' top -n 1 ' command NoopurTiwari Linux - Newbie 1 05-29-2007 03:45 AM
Schedule TOP command in Crontab. imsajjadali Red Hat 2 05-20-2004 05:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration