# how to check diskspace?



## wonslung (Oct 12, 2009)

I have a little script i wrote to check if a process is running, and start it if it isn't.  This has worked fine but now i have a new issue.

I need to be able to check the space of a users home directory and make the script NOT run the process if it's close to full....any ideas? i'm fairly new to this.

Here is my current script

```
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
SERVICE='rtorrent'

if pgrep -u $USER $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    echo "$SERVICE is not running, starting $SERVICE"| screen -d -m -S seedbox $SERVICE
fi
```


----------



## aragon (Oct 12, 2009)

wonslung said:
			
		

> close to full


First you need to define that.

Once defined, you'd use either df(1) or du(1) I imagine


----------



## wonslung (Oct 12, 2009)

```
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }'
```

is what i have so far.....


----------



## wonslung (Oct 12, 2009)

ok, so i have this

```
#!/bin/sh
df -H /usr/home/$USER| grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge 90 ]; then
    echo "out of space \"$partition ($usep%)\" on $(hostname) as on $(date)"
  fi
done
```

now i need to make it not not run rtorrent if it's over the number i set, currently i have it at 90


----------



## wonslung (Oct 12, 2009)

```
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin

df -H /usr/home/$USER| grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge 90 ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)"
  fi
done
SERVICE='rtorrent'

if pgrep -u $USER $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    echo "$SERVICE is not running, starting $SERVICE"| screen -d -m -S seedbox $SERVICE
fi
```
i think this will work, what do you think


----------



## wonslung (Oct 12, 2009)

actually, this doesn't work.....i'm so new to this...how do i make it END before it checks the rtorrent part


----------



## wonslung (Oct 12, 2009)

```
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
SERVICE='rtorrent'

df -H /usr/home/$USER| grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge 70 ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)"
 else


if pgrep -u $USER $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    echo "$SERVICE is not running, starting $SERVICE"| screen -d -m -S seedbox $SERVICE
 fi
fi
done
```
ok, i got it


----------



## aragon (Oct 12, 2009)

Shew, let's simplify it a bit...


```
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin

df /usr/home/$USER| tail -1 | while read fs size used avail pcnt mount;
do
  pcnt=$(echo ${pcnt} | cut -d'%' -f1 )
  if [ ${pcnt} -ge 90 ]; then
    echo "Running out of space \"${fs} (${pcnt}%)\" on ${HOSTNAME} as on $(date)"
    exit 1
  fi
done
SERVICE='rtorrent'

if pgrep -u $USER $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    echo "$SERVICE is not running, starting $SERVICE"| screen -d -m -S seedbox $SERVICE
fi
```


----------



## wonslung (Oct 12, 2009)

heh, i'm pretty new to this....i'm sure i'm making a lot of rookie mistakes,. i do appreciate the help
never seen tail before

btw, yours doesn't work....it starts rtorrent even if the space is regestered too high
mine seems to work...


----------



## wonslung (Oct 12, 2009)

```
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
SERVICE='rtorrent'

df /usr/home/$USER| tail -1 | while read fs size used avail pcnt mount;
do
  pcnt=$(echo ${pcnt} | cut -d'%' -f1 )
  if [ ${pcnt} -ge 70 ]; then
    echo "Running out of space \"${fs} (${pcnt}%)\" on ${HOSTNAME} as on $(date)"
    exit 1
  fi

if pgrep -u $USER $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    echo "$SERVICE is not running, starting $SERVICE"| screen -d -m -S seedbox $SERVICE
fi
done
```
moving the DONE to the end makes your work


----------



## aragon (Oct 12, 2009)

Ah yes, the while block is executing in a subshell.  Adding this after done should also fix it:


```
if [ $? -eq 1 ]; then exit 1; fi
```


----------



## wonslung (Oct 12, 2009)

this is so cool....i love unix

I wanted to set rtorrent to automatically stop if diskspace is full because it makes the system go nuts otherwise...this is really cool...

i need to figure out something similar for another system which uses quota instead of actual partitions but i think i already have a general idea of how to do it now...this really makes my day, thanks a lot man

see, i can put this in each users crontab as an @reboot and every 5 minutes...now if it stops for some reason other than disk space it will start back up...this is so cool


----------



## aragon (Oct 12, 2009)

wonslung said:
			
		

> i love unix


ditto 

You're welcome.


----------



## DutchDaemon (Oct 12, 2009)

wonslung said:
			
		

> I wanted to set rtorrent to automatically stop if diskspace is full because it makes the system go nuts otherwise...



What's wrong with using the available tool for that, .rorrent.rc?


```
# Close torrents when diskspace is low.
#schedule = low_diskspace,5,60,close_low_diskspace=100M
```


----------



## wonslung (Oct 13, 2009)

DutchDaemon said:
			
		

> What's wrong with using the available tool for that, .rorrent.rc?
> 
> 
> ```
> ...



that's what i use to CLOSE rtorrent.


This script isn't used to close rtorrent, it's used to start it.  each user runs it via crontab @reboot

The problem i was having was that if even one user fills up his home directory, the entire system would go nuts.  (this box has 4 users each with thier home on a separate slice)

rtorrent also doesn't come with a script to start at boot, and all the ones on the rtorrent site are for linux, so when i needed to make rtorrent start @boot i had the choice of trying to write a very complicated script to start it up for all users or just write a simple one for cron.

Well the original script didn't check for disk space, which caused problems.  Now it does.


----------



## wonslung (Oct 14, 2009)

ok, i got a new one...i want to check if a file exists and delete it, but ONLY if rtorrent is NOT running, and then i want it to start.  I STILL want it to start if it DOESNT find the file...i must be doing something wrong....

i figure i can use something like this....


```
FILE="/usr/home/$USER/rtorrent/.sock/scgi.socket"
    if test -f $FILE
    then
       rm $FILE
```

but how do i make it work with my current script?

```
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
SERVICE='rtorrent'

df /usr/home/$USER| tail -1 | while read fs size used avail pcnt mount;
do
  pcnt=$(echo ${pcnt} | cut -d'%' -f1 )
  if [ ${pcnt} -ge 95 ]; then
    echo "Running out of space \"${fs} (${pcnt}%)\" on ${HOSTNAME} as on $(date)"
    exit 1
  fi

if pgrep -u $USER $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    echo "$SERVICE is not running, starting $SERVICE"| screen -d -m -S seedbox $SERVICE
fi
done
```


i want it to ONLY check for this files existence IF rtorrent isn't running, then delete it, then start rtorrent.

I also want it to start rtorrent if it is not running and it DOES NOT find this file....how do i do those types of things?

i'm kind of hitting a wall


----------



## DutchDaemon (Oct 14, 2009)

Try going about it 'from the other end'. Don't check for the file, check for the process.

1. if rtorrent is running for a particular user, don't do anything
2. if rtorrent is not running for a particular user, remove the socket (whether it exists or not, use rm -f to avoid getting an error), and start rtorrent.


----------



## aragon (Oct 15, 2009)

If you intend to delete a file, there is probably no need to first check for its existence.  Simply try delete it and ignore any errors.


```
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
SERVICE='rtorrent'
FILE="/usr/home/$USER/rtorrent/.sock/scgi.socket"

df /usr/home/$USER| tail -1 | while read fs size used avail pcnt mount;
do
  pcnt=$(echo ${pcnt} | cut -d'%' -f1 )
  if [ ${pcnt} -ge 95 ]; then
    echo "Running out of space \"${fs} (${pcnt}%)\" on ${HOSTNAME} as on $(date)"
    exit 1
  fi

if pgrep -u $USER $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    rm -f ${FILE}
    echo "$SERVICE is not running, starting $SERVICE"| screen -d -m -S seedbox $SERVICE
fi
done
```


----------



## wonslung (Oct 15, 2009)

thanks to both of you....so simple that i feel silly for asking.


----------



## wonslung (Oct 15, 2009)

DutchDaemon said:
			
		

> Try going about it 'from the other end'. Don't check for the file, check for the process.
> 
> 1. if rtorrent is running for a particular user, don't do anything
> 2. if rtorrent is not running for a particular user, remove the socket (whether it exists or not, use rm -f to avoid getting an error), and start rtorrent.



this makes a lot of sense...i wasn't thinking about the -f option...thanks again....since i already check for the process this works easily..thanks again.


----------

