• 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.

  • 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
    /dev/mapper/VolGroup-root
                          6.8G  4.3G  2.2G  66% /
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-root
                          6.8G  4.3G  2.2G  66% /
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-root
                          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

    Deepak Prasad

    is a techie and an author who is still trying to survive in this IT generation with very little knowledge he has on Linux/Unix, VMware, SAN Storage, Automation, networking etc

    You can follow him on Facebook or Google+

    Do you also have something to share here?

    Join GoLinuxHub Team as an Author, Click here for more information
    How to run any script or cron job in Linux for every second How to run any script or cron job in Linux for every second Reviewed by Deepak Prasad on Thursday, June 19, 2014 Rating: 5

    3 comments:

    Powered by Blogger.