![]() |
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? |
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 Code:
export PATH=/bin:/usr/bin |
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. |
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 |
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? |
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. |