• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • Life always offers you a second chance ... Its called tomorrow !!!

    Thursday, June 19, 2014

    How to run any script or cron job in Linux for every second

    There was this one time when I ran into the situation when my /tmp partition was getting full at 100% and I got an threshold alert but again when I checked the same it was never utilizing  more than 20%. So I planned to monitor the changes inside my /tmp directory in seconds so that I can find out the exact time for the spike and threshold alert.

    Now this was one scenario I was into, there can be others where you need to monitor an event on a per second wise so what should we do?

    Here is what I did.

    Created a cron job entry for every second

    To edit the cronjobs use the below command
    # crontab -e
    * * * * *
    while true; do df -h /tmp >> /root/filesystem.txt & sleep 1; done
    Save and exit
    Here the output of df -h /tmp will be stored inside /root/filesystem.txt every second.

    Now lets monitor the output. For live feed and changes inside any file use the below command
    # tail -f /root/filesystem.txt
                          6.8G  4.3G  2.2G  66% /
    Filesystem            Size  Used Avail Use% Mounted on
                          6.8G  4.3G  2.2G  66% /
    Filesystem            Size  Used Avail Use% Mounted on
                          6.8G  4.3G  2.2G  66% /
    Filesystem            Size  Used Avail Use% Mounted on
                          6.8G  4.3G  2.2G  66% /

    So, as you see the above command started showing me live output, using which I was able to identify the time of the spike and the rest of the story is not relevant to this article.

    But the bottom line is you can use the above script to run any of your cron jobs for every second.

    The same if you want to change for 5 second, just adjust the sleep value as per your requirement.

    Another example
    # crontab -e
    * * * * *
    while true; do date >> /root/date.txt &  sleep 1; done

    # tail -f /root/date.txt
    Thu Jun 19 11:12:24 IST 2014
    Thu Jun 19 11:12:25 IST 2014
    Thu Jun 19 11:12:26 IST 2014
    Thu Jun 19 11:12:27 IST 2014
    Thu Jun 19 11:12:28 IST 2014
    Thu Jun 19 11:12:29 IST 2014
    Thu Jun 19 11:12:30 IST 2014
    Thu Jun 19 11:12:31 IST 2014
    Thu Jun 19 11:12:32 IST 2014

    I hope I made my self clear

    Related Articles:
    How to track all the successful and failed login attempts by users in Linux
    How to check last login time for users in Linux
    How to create user without useradd command in Linux
    How to give permission to user to run some commands in Linux

    Follow the below links for more tutorials

    Step by Step Linux Boot Process Explained In Detail
    RAID levels 0, 1, 2, 3, 4, 5, 6, 0+1, 1+0 features explained in detail
    Tutorial for Monitoring Tools SAR and KSAR with examples in Linux
    How to secure Apache web server in Linux using password (.htaccess)
    How to register Red Hat Linux with RHN (Red Hat Network )
    15 tips to enhance security of your Linux machine
    How does a DNS query works when you type a URL on your browser?
    How to create password less ssh connection for multiple non-root users
    How to create user without useradd command in Linux
    How to give normal user root privileges using sudo in Linux/Unix
    How to do Ethernet/NIC bonding/teaming in Red Hat Linux
    How to install/uninstall/upgrade rpm package with/without dependencies
    Why is Linux more secure than windows and any other OS
    What is the difference between "su" and "su -" in Linux?
    How to secure boot loader (grub menu) with password in RHEL 6
    Understanding Load Average in Linux and when to be worried about it?


    Post a Comment