LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   No output from top command run by crontab (https://www.linuxquestions.org/questions/linux-newbie-8/no-output-from-top-command-run-by-crontab-881184/)

Gr1f1S 05-17-2011 07:08 AM

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?

MensaWater 05-17-2011 08:55 AM

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.

SL00b 05-17-2011 09:02 AM

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.

Gr1f1S 05-17-2011 09:48 AM

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

MensaWater 05-17-2011 10:00 AM

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?

eng_michael_magdy 04-23-2013 05:47 AM

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


All times are GMT -5. The time now is 02:20 PM.