Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2005
    Location
    Moonachie, New Jersey
    Posts
    7

    top output to file on cron plzz help

    I thought i had resolved the problem only to find the the script only works correctly if i run it manually.
    I entered it to run with a cron job to run every 5 minutes and it does run every 5 minutes. It also gives me part of the output i request but it will not give the "top" output. this is what i did for starters

    while in root i entered a cron job using
    crontab -e

    this is the entry:

    */5 * * * * /usr/local/bin/ht_stat.sh a > /dev/null

    This is the script i created:

    # This script has been giving uptime,date and httpd info every 5 minutes since sept. 01,'05.

    echo `date` >> /home/rau/htstat/htstat.txt

    uptime >> /home/rau/htstat/htstat.txt

    echo "HTTPD `ps -f | grep httpd | wc -l`" >> /home/rau/htstat/htstat.txt

    echo "" >> /home/rau/htstat/htstat.txt

    #I'm adding these 2 lines of code to get a top reading in the same intervals and put a line of space between the orginal reading and the top readings.

    top -n 1 >> /home/rau/htstat/htstat.txt
    echo "" >> /home/rau/htstat/htstat.txt

    This is my output if the cron run the job:

    Fri Sep 16 09:55:01 EDT 2005
    09:55:01 up 56 days, 21:31, 115 users, load average: 0.53, 0.48, 0.37
    HTTPD 2


    Fri Sep 16 10:00:00 EDT 2005
    10:00:00 up 56 days, 21:36, 115 users, load average: 0.38, 0.41, 0.36
    HTTPD 2


    Fri Sep 16 10:05:00 EDT 2005
    10:05:00 up 56 days, 21:41, 115 users, load average: 0.06, 0.22, 0.29
    HTTPD 2

    If i run the the job manually:

    ./ht_stat.sh

    I get the desired output like so:


    Fri Sep 16 10:25:35 EDT 2005
    10:25:35 up 56 days, 22:02, 115 users, load average: 0.18, 0.60, 0.58
    HTTPD 1
    top - 10:25:35 up 56 days, 22:02, 115 users, load average: 0.18, 0.60, 0.58
    Tasks: 163 total, 1 running, 162 sleeping, 0 stopped, 0 zombie
    Cpu(s): 3.8% us, 3.2% sy, 5.7% ni, 87.0% id, 0.2% wa, 0.0% hi, 0.0% si
    Mem: 516684k total, 469844k used, 46840k free, 169848k buffers
    Swap: 1052248k total, 65216k used, 987032k free, 90188k cached

    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
    10745 root 18 0 1984 892 1620 R 1.9 0.2 0:00.03 top
    1 root 16 0 2704 292 1316 S 0.0 0.1 0:03.31 init
    2 root 34 19 0 0 0 S 0.0 0.0 0:00.10 ksoftirqd/0
    3 root 5 -10 0 0 0 S 0.0 0.0 0:04.36 events/0
    4 root 5 -10 0 0 0 S 0.0 0.0 0:00.02 khelper
    5 root 13 -10 0 0 0 S 0.0 0.0 0:00.00 kacpid
    20 root 5 -10 0 0 0 S 0.0 0.0 0:00.00 kblockd/0
    21 root 15 0 0 0 0 S 0.0 0.0 0:00.00 khubd
    33 root 15 0 0 0 0 S 0.0 0.0 0:27.96 pdflush
    35 root 7 -10 0 0 0 S 0.0 0.0 0:00.00 aio/0
    34 root 15 0 0 0 0 S 0.0 0.0 0:12.32 kswapd0

    184 root 15 0 0 0 0 S 0.0 0.0 0:07.93 kjournald
    668 root 15 0 0 0 0 S 0.0 0.0 0:00.00 kjournald
    669 root 15 0 0 0 0 S 0.0 0.0 0:00.30 kjournald
    670 root 15 0 0 0 0 S 0.0 0.0 0:00.19 kjournald
    671 root 15 0 0 0 0 S 0.0 0.0 0:05.28 kjournald
    672 root 15 0 0 0 0 S 0.0 0.0 0:09.25 kjournald
    1066 root 16 0 2568 572 1648 S 0.0 0.1 0:26.30 dhclient
    1097 root 16 0 3064 380 1296 S 0.0 0.1 0:04.09 syslogd
    1101 root 16 0 2500 252 1244 S 0.0 0.0 0:00.04 klogd
    1138 rpcuser 22 0 3252 272 1380 S 0.0 0.1 0:00.00 rpc.statd
    1163 root 16 0 2580 300 1296 S 0.0 0.1 0:01.60 rpc.idmapd
    1203 root 18 0 2252 268 1532 S 0.0 0.1 0:00.00 hcid
    1210 root 10 -10 0 0 0 S 0.0 0.0 0:00.00 krfcommd
    1229 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rpciod
    1230 root 21 0 0 0 0 S 0.0 0.0 0:00.00 lockd
    1235 root 17 0 8560 292 6704 S 0.0 0.1 0:00.00 mount.smbfs

    You have new mail in /var/spool/mail/root

    I just don't know why it works one way and not the other, somebody pleezz help me - caligal

  2. #2
    Join Date
    Aug 2005
    Location
    Canada
    Posts
    862
    Maybe the $PATH isn't the same in cronjob.
    Put "printenv|sort" type of thing to check the env vars.
    Use full path to commands.

    Also, some commands output data to stderr.
    Add "exec 2>&1" to the beginning
    or instead of just > /dev/null, 2>>/path-to/error.log
    or something so that you get stderr.

  3. #3
    Do you see any errors in the cron log file /var/log/cron when your script executes ?

    Instead of trying this

    */5 * * * * /usr/local/bin/ht_stat.sh a > /dev/null

    you may also try this

    */5 * * * * /usr/local/bin/ht_stat.sh a > /dev/null 2>&1

    Regards,
    Rose
    rose@instacarma.com
    Regards,
    Rose [rose@instacarma.com]
    InstaCarma.com
    24x7 Technical Support and Server Management

  4. #4
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,569
    tried running top in batch mode (-b) ?

  5. #5
    Join Date
    Aug 2005
    Location
    Canada
    Posts
    862
    Guys, it's an old old thread....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •