Why cron job fails to redirect top command output, to a file, using a shell script?

Solution Verified - Updated -

Issue

  • A cron job fails to redirect top command output, to a file, using a shell script.

    # cat /scripts/cpu-usage.sh
    !/bin/bash
    date >> /tmp/stats/cpu_stats
    top |head -n 3 |tail -n 1 >> /tmp/stats/cpu_stats
    
    # grep cpu /etc/crontab
    */5 * * * * root /scripts/cpu-usage.sh
    
    # tail -f /tmp/stats/cpu_stats
    Mon Jul 17 21:05:01 IST 2023
    Mon Jul 17 21:10:01 IST 2023
    Mon Jul 17 21:15:01 IST 2023
    

Environment

  • Red Hat Enterprise Linux 6

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content