#!/bin/sh LOCK_FILE=/tmp/.snail.nvrun.lock NVRUN=/usr/local/bin/nvrun ( for i in `seq 8`; do flock -n 9 if [[ 0 -ne $? && 5 -eq $i ]]; then echo "Cann't lock $COUNT_FILE. Exiting..." exit -1 elif [ 0 -eq $? ]; then break fi sleep 1 done snail.nvstat || snail.nvon if [ 0 -ne $? ]; then echo "Cann't switch on nVidia graphics. Exiting..." exit -1 fi ) 9>$LOCK_FILE || exit $? snail.nvrun "$@" ( for i in `seq 8`; do flock -n 9 if [[ 0 -ne $? && 5 -eq $i ]]; then echo "Cann't lock $COUNT_FILE. Exiting..." exit -1 elif [ 0 -eq $? ]; then break fi sleep 1 done pgrep -f $NVRUN &>/dev/null if [ 0 -eq $? ]; then echo "Other applications still use nVidia chip. So we aren't switching off it, just exiting..." exit 0 else snail.nvoff [ 0 -ne $? ] && echo "Error: Cann't switch off nVidia graphics. Exiting..." && exit -1 echo "nVidia chip powered off." exit 0 fi ) 9>$LOCK_FILE exit $?