martes, 25 de agosto de 2009

Parar una aplicacion cuando otras se ejecutan

#!/bin/bash
# Por Paco Aldarias.
# 23.8.2009
# idle.sh
# Este scripts en bash permite activar
# mldonkey cuando esta idle o inactivo
# Siendo el estado activo, cuando se ejecuta firefox xql etc


apli="firefox xql et.x86"
temp="/tmp/idle.txt"
ml="/home/paco/mldonkey.sh"
flog="/tmp/idle.log"
log=1

[ -f $flog ] && rm -f $flog
[ $log -eq 1 ] && echo "Arrancado: $(date +%H:%M:%S) $flog" >> $flog

# tiempos en segundos
tini=3m
tidle=1m
tbusy=5m

# Limpiamos si esta en ejecución el script.
if [ -f $temp ];then
kill $(cat $temp)
rm -f $temp
fi

# Si esta en marcha paramos ml
proc=$(ps aux | grep mlnet | grep -v grep | wc -l)
[ $proc -gt 0 ] && $ml stop
[ $1 ] && [ $1 = "stop" ] && exit

sleep $tini

# Bucle infinito
while :
do
descargar=1

# Hacemos esto por cada aplicacion que queremos que para ml
for i in $(echo $apli)
do
if [ $descargar -eq 1 ];then
up=$(ps aux | grep "$i" | grep -v grep | awk '{print $11}')
if [ $up ];then
descargar=0
[ $log -eq 1 ] && echo "UP: $up $i">> $flog
fi
fi
done

# Descargamos. Ponemos en marcha mldonkey
if [ $descargar -eq 1 ]
then
[ $log -eq 1 ] && echo "$(date +%H:%M:%S) Podemos descargar" >> $flog
if [ ! -f $temp ];then
echo $$ > $temp
sh $ml start
fi
sleep $tidle
# Apagamos. Paramos mldonkey
else
[ $log -eq 1 ] && echo "$(date +%H:%M:%S) No podemos descargar" >> $flog
if [ -f $temp ];then
sh $ml stop
rm -f $temp
fi
sleep $tbusy
fi

done

No hay comentarios:

Publicar un comentario