auto-start cron script

shwetha17

Member
May 24, 2018
785
0
16
This is a little script i wrote which does 2 things.

First, it checks how much space is left in the users home directory (if you use zfs quotas or separate slices this is great)

If it finds there is enough space, it will check to see if rtorrent is already running, if it IST running, it will remove the lock file (if it is there) and start rtorrent.
If it is running it will exit.

Put this in cron as @reboot and or every 5 minutes to make sure rtorrent stays running. Also, add a line to .rtorrent.rc to make rtorrent automatically stop when close to full.


note: on some systems the script ITSELF will trigger the check IF it has the word "rtorrent" in the name so just name it something like chktorrent or whatever you like.
Remember to edit the variables to suit your needs
Code: [Select]
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
SERVICE='rtorrent'
FILE="/usr/home/$USER/rtorrent/.session/rtorrent.lock"
HOMEDIR="/usr/home/$USER"
SIZE=95 #total %age

df $HOMEDIR| tail -1 | while read fs size used avail pcnt mount;
do
pcnt=$(echo ${pcnt} | cut -d'%' -f1 )
if [ ${pcnt} -ge $SIZE ]; 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
 

jith45

Member
May 25, 2018
960
0
16
If you are running this script from cron (at least in ubuntu) you also need to define the $USER variable.

so just put in

USER='username'

above SERVICE='rtorrent'
 

dsouvik215

Member
May 25, 2018
896
0
16
I never had to do this on ubuntu or debian.


Also, setting it the way you recommend defeats the purpose of how i designed the script. Your method would require a new script for each user.

Cron should be able to pull $USER env data from the /etc/passwd file. Something is wrong with your system.


check:

http://manpages.ubuntu.com/manpages/lucid/man5/bcrontab.5.html


note this part:



Quote
Several environment variables are set up automatically by the
bcron-exec(cool.gif program. SHELL is set to /bin/sh, and LOGNAME, USER, and
HOME are set from the /etc/passwd line of the crontab’s owner.​


maybe you should try changing $USER to $LOGNAME
 

somus1735

Member
May 25, 2018
833
0
16
Just to confirm changing $USER to $LOGNAME works on my system.

Most likely is something wrong on my setup, $USER and $LOGNAME should work...
 

somus1735

Member
May 25, 2018
833
0
16
BTW, env vars in cron are best set at the top of the crontab.

Doing something like

SIZE=${1:-95}

also allows different thresholds, while still sharing the script.