Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9269369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:06:10+00:00 2026-06-18T15:06:10+00:00

I have tried writing a shell script which alerts admin when the disk usage

  • 0

I have tried writing a shell script which alerts admin when the disk usage reaches up to 70%. I want write this script in python

#!/bin/bash
ADMIN="admin@myaccount.com"
INFORM=70

df -H  | grep -vE ‘^Filesystem|tmpfs|cdrom’ | awk ‘{ print $5 ” ” $6 }’ | while read   output;
do
use=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1  )
partition=$(echo $output | awk ‘{ print $2 }’ )
if [ $use -ge $INFORM ]; then
echo “Running out of space \”$partition ($use%)\” on $(hostname) as on $(date)” |
mail -s “DISK SPACE ALERT: $(hostname)” $ADMIN
fi
done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:06:11+00:00Added an answer on June 18, 2026 at 3:06 pm

    The easiest(understandable) approach for you would be to run the df command on an external process and extract the details from the returned output.

    To execute a shell command in Python, you need to use subprocess module. You can use smtplib module to send emails to the admin.

    I cooked up a small script that should do the job of filtering the filesystems you don’t need to monitor, does some string manipulation to pull out the filesystem and % used values and prints out when the usage exceeds the threshold.

    #!/bin/python
    import subprocess
    import datetime
    
    IGNORE_FILESYSTEMS = ('Filesystem', 'tmpfs', 'cdrom', 'none')
    LIMIT = 70
    
    def main():
      df = subprocess.Popen(['df', '-H'], stdout=subprocess.PIPE)
      output = df.stdout.readlines()
      for line in output:
        parts = line.split()
        filesystem = parts[0]
        if filesystem not in IGNORE_FILESYSTEMS:
          usage = int(parts[4][:-1])  # Strips out the % from 'Use%'
          if usage > LIMIT:
            # Use smtplib sendmail to send an email to the admin.
            print 'Running out of space %s (%s%%) on %s"' % (
                filesystem, usage, datetime.datetime.now())
    
    
    if __name__ == '__main__':
      main()
    

    The output of the executed script would be something like this:

    Running out of space /dev/mapper/arka-root (72%) on 2013-02-11 02:11:27.682936
    Running out of space /dev/sda1 (78%) on 2013-02-11 02:11:27.683074
    Running out of space /dev/mapper/arka-usr+local (81%) on 2013-02-11 02:11
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is in Access 2007. I have tried writing somethig like: Forms!MyForm!TextBox = Count(ISNUMERIC(ISNUMERIC(QueryResult.Reading)))
I'm writing a shell script which iterates over a set of variables, edits a
I have tried the following program for writing the contents into Spreadsheet. I downloaded
So I have tried to do this extensively but to no avail.. I'm writting
Have tried to find solutions for this and can't really come up with anything.
I have tried searching over the internet about this problem but not able to
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I bumped into the following problem: I'm writing a Linux bash script which does
I'm writing a bit of code to run a shell script using process that
I'm writing a shell script that will perform a pg_dump from one server, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.