[SOLVED]Daemon won't start as a daemon - Fancontrol problems

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Burrito

[SOLVED]Daemon won't start as a daemon - Fancontrol problems

Post by Burrito »

Hi,

I've installed a fancontrol script from the Ubuntu repository, and modified it a little because of problems I've had with it (sometimes it would act like the config is wrong when it would in fact work if it just ignored the issues, and sometimes the problem would be solved by running a modprobe command which it could actually do by itself). I've attempted to set up a script to run it as a daemon at startup, with admin privileges and all so it can modify the PWM settings, but that didn't work. When I try to run "sudo /etc/init.d/fancontrol start" it returns " * Starting fancontrol daemon... [ OK ] " but the fancontrol process isn't present, and the fans are still at full speed.
When I start it manually (sudo fancontrol), it works okay.
I've now tried to reinstall it, thereby undoing all my modifications. But the daemon problem persists.

The tutorial I used to try and get this working is this: http://ubuntuforums.org/showthread.php?t=42737
Except I used the version from the package manager.

My fancontrol (at /usr/sbin/fancontrol) script looks like this, after being reinstalled:

Code: Select all

#!/bin/bash
#
# Simple script implementing a temperature dependent fan speed control
# Supported Linux kernel versions: 2.6.5 and later
#
# Version 0.70
#
# Usage: fancontrol [CONFIGFILE]
#
# Dependencies:
#   bash, egrep, sed, cut, sleep, readlink, lm_sensors :)
#
# Please send any questions, comments or success stories to
# marius.reiner@hdev.de
# Thanks!
#
# For configuration instructions and warnings please see fancontrol.txt, which
# can be found in the doc/ directory or at the website mentioned above.
#
#
#    Copyright 2003 Marius Reiner <marius.reiner@hdev.de>
#    Copyright (C) 2007-2009 Jean Delvare <khali@linux-fr.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#    MA 02110-1301 USA.
#
#

PIDFILE="/var/run/fancontrol.pid"

#DEBUG=1
MAX=255

declare -i pwmval

function LoadConfig {
	echo "Loading configuration from $1 ..."
	if [ ! -r "$1" ]
	then
		echo "Error: Can't read configuration file"
		exit 1
	fi

	# grep configuration from file
	INTERVAL=`egrep '^INTERVAL=.*$' $1 | sed -e 's/INTERVAL=//g'`
	DEVPATH=`egrep '^DEVPATH=.*$' $1 | sed -e 's/DEVPATH= *//g'`
	DEVNAME=`egrep '^DEVNAME=.*$' $1 | sed -e 's/DEVNAME= *//g'`
	FCTEMPS=`egrep '^FCTEMPS=.*$' $1 | sed -e 's/FCTEMPS=//g'`
	MINTEMP=`egrep '^MINTEMP=.*$' $1 | sed -e 's/MINTEMP=//g'`
	MAXTEMP=`egrep '^MAXTEMP=.*$' $1 | sed -e 's/MAXTEMP=//g'`
	MINSTART=`egrep '^MINSTART=.*$' $1 | sed -e 's/MINSTART=//g'`
	MINSTOP=`egrep '^MINSTOP=.*$' $1 | sed -e 's/MINSTOP=//g'`
	# optional settings:
	FCFANS=`egrep '^FCFANS=.*$' $1 | sed -e 's/FCFANS=//g'`
	MINPWM=`egrep '^MINPWM=.*$' $1 | sed -e 's/MINPWM=//g'`
	MAXPWM=`egrep '^MAXPWM=.*$' $1 | sed -e 's/MAXPWM=//g'`

	# Check whether all mandatory settings are set
	if [[ -z ${INTERVAL} || -z ${FCTEMPS} || -z ${MINTEMP} || -z ${MAXTEMP} || -z ${MINSTART} || -z ${MINSTOP} ]]
	then
		echo "Some mandatory settings missing, please check your config file!"
		exit 1
	fi
	if [ "$INTERVAL" -le 0 ]
	then
		echo "Error in configuration file:"
		echo "INTERVAL must be at least 1"
		exit 1
	fi

	# write settings to arrays for easier use and print them
	echo
	echo "Common settings:"
	echo "  INTERVAL=$INTERVAL"

	let fcvcount=0
	for fcv in $FCTEMPS
	do
		if ! echo $fcv | egrep -q '='
		then
			echo "Error in configuration file:"
			echo "FCTEMPS value is improperly formatted"
			exit 1
		fi

		AFCPWM[$fcvcount]=`echo $fcv |cut -d'=' -f1`
		AFCTEMP[$fcvcount]=`echo $fcv |cut -d'=' -f2`
		AFCFAN[$fcvcount]=`echo $FCFANS |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
		AFCMINTEMP[$fcvcount]=`echo $MINTEMP |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
		AFCMAXTEMP[$fcvcount]=`echo $MAXTEMP |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
		AFCMINSTART[$fcvcount]=`echo $MINSTART |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
		AFCMINSTOP[$fcvcount]=`echo $MINSTOP |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
		AFCMINPWM[$fcvcount]=`echo $MINPWM |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
		[ -z "${AFCMINPWM[$fcvcount]}" ] && AFCMINPWM[$fcvcount]=0
		AFCMAXPWM[$fcvcount]=`echo $MAXPWM |sed -e 's/ /\n/g' |egrep "${AFCPWM[$fcvcount]}" |cut -d'=' -f2`
		[ -z "${AFCMAXPWM[$fcvcount]}" ] && AFCMAXPWM[$fcvcount]=255

		# verify the validity of the settings
		if [ "${AFCMINTEMP[$fcvcount]}" -ge "${AFCMAXTEMP[$fcvcount]}" ]
		then
			echo "Error in configuration file (${AFCPWM[$fcvcount]}):"
			echo "MINTEMP must be less than MAXTEMP"
			exit 1
		fi
		if [ "${AFCMAXPWM[$fcvcount]}" -gt 255 ]
		then
			echo "Error in configuration file (${AFCPWM[$fcvcount]}):"
			echo "MAXPWM must be at most 255"
			exit 1
		fi
		if [ "${AFCMINSTOP[$fcvcount]}" -ge "${AFCMAXPWM[$fcvcount]}" ]
		then
			echo "Error in configuration file (${AFCPWM[$fcvcount]}):"
			echo "MINSTOP must be less than MAXPWM"
			exit 1
		fi
		if [ "${AFCMINSTOP[$fcvcount]}" -lt "${AFCMINPWM[$fcvcount]}" ]
		then
			echo "Error in configuration file (${AFCPWM[$fcvcount]}):"
			echo "MINSTOP must be greater than or equal to MINPWM"
			exit 1
		fi
		if [ "${AFCMINPWM[$fcvcount]}" -lt 0 ]
		then
			echo "Error in configuration file (${AFCPWM[$fcvcount]}):"
			echo "MINPWM must be at least 0"
			exit 1
		fi

		echo
		echo "Settings for ${AFCPWM[$fcvcount]}:"
		echo "  Depends on ${AFCTEMP[$fcvcount]}"
		echo "  Controls ${AFCFAN[$fcvcount]}"
		echo "  MINTEMP=${AFCMINTEMP[$fcvcount]}"
		echo "  MAXTEMP=${AFCMAXTEMP[$fcvcount]}"
		echo "  MINSTART=${AFCMINSTART[$fcvcount]}"
		echo "  MINSTOP=${AFCMINSTOP[$fcvcount]}"
		echo "  MINPWM=${AFCMINPWM[$fcvcount]}"
		echo "  MAXPWM=${AFCMAXPWM[$fcvcount]}"
		let fcvcount=fcvcount+1
	done
	echo
}

function DevicePath()
{
	if [ -h "$1/device" ]
	then
		readlink -f "$1/device" | sed -e 's/^\/sys\///'
	fi
}

function DeviceName()
{
	if [ -r "$1/name" ]
	then
		cat "$1/name" | sed -e 's/[[:space:]=]/_/g'
	elif [ -r "$1/device/name" ]
	then
		cat "$1/device/name" | sed -e 's/[[:space:]=]/_/g'
	fi
}

function ValidateDevices()
{
	local OLD_DEVPATH="$1" OLD_DEVNAME="$2" outdated=0
	local entry device name path

	for entry in $OLD_DEVPATH
	do
		device=`echo "$entry" | sed -e 's/=[^=]*$//'`
		path=`echo "$entry" | sed -e 's/^[^=]*=//'`

		if [ "`DevicePath "$device"`" != "$path" ]
		then
			echo "Device path of $device has changed"
			outdated=1
		fi
	done

	for entry in $OLD_DEVNAME
	do
		device=`echo "$entry" | sed -e 's/=[^=]*$//'`
		name=`echo "$entry" | sed -e 's/^[^=]*=//'`

		if [ "`DeviceName "$device"`" != "$name" ]
		then
			echo "Device name of $device has changed"
			outdated=1
		fi
	done

	return $outdated
}

# Check that all referenced sysfs files exist
function CheckFiles {
	local outdated=0

	let fcvcount=0
	while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
	do
		pwmo=${AFCPWM[$fcvcount]}
		if [ ! -w $pwmo ]
		then
			echo "Error: file $pwmo doesn't exist"
			outdated=1
		fi
		let fcvcount=$fcvcount+1
	done

	let fcvcount=0
	while (( $fcvcount < ${#AFCTEMP[@]} )) # go through all temp inputs
	do
		tsen=${AFCTEMP[$fcvcount]}
		if [ ! -r $tsen ]
		then
			echo "Error: file $tsen doesn't exist"
			outdated=1
		fi
		let fcvcount=$fcvcount+1
	done

	let fcvcount=0
	while (( $fcvcount < ${#AFCFAN[@]} )) # go through all fan inputs
	do
		fan=${AFCFAN[$fcvcount]}
		if [ ! -r $fan ]
		then
			echo "Error: file $fan doesn't exist"
			outdated=1
		fi
		let fcvcount=$fcvcount+1
	done

	if [ $outdated -eq 1 ]
	then
		echo
		echo "At least one referenced file is missing. Either some required kernel"
		echo "modules haven't been loaded, or your configuration file is outdated."
		echo "In the latter case, you should run pwmconfig again."
	fi

	return $outdated
}

if [ "$1" == "--check" ]
then
	if [ -f "$2" ]
	then
		LoadConfig $2
	else
		LoadConfig /etc/fancontrol
	fi
	exit 0
fi

if [ -f "$1" ]
then
	LoadConfig $1
else
	LoadConfig /etc/fancontrol
fi

# Detect path to sensors
if echo "${AFCPWM[0]}" | egrep -q '^/'
then
	DIR=/
elif echo "${AFCPWM[0]}" | egrep -q '^hwmon[0-9]'
then
	DIR=/sys/class/hwmon
elif echo "${AFCPWM[0]}" | egrep -q '^[1-9]*[0-9]-[0-9abcdef]{4}'
then
	DIR=/sys/bus/i2c/devices
else
	echo "$0: Invalid path to sensors"
	exit 1
fi

if [ ! -d $DIR ]
then
	echo $0: 'No sensors found! (did you load the necessary modules?)'
	exit 1
fi
cd $DIR

# Check for configuration change
if [ -z "$DEVPATH" -o -z "$DEVNAME" ]
then
	echo "Configuration is too old, please run pwmconfig again"
	exit 1
fi
if ! ValidateDevices "$DEVPATH" "$DEVNAME"
then
	echo "Configuration appears to be outdated, please run pwmconfig again"
	exit 1
fi
CheckFiles || exit 1

if [ -f "$PIDFILE" ]
then
	echo "File $PIDFILE exists, is fancontrol already running?"
	exit 1
fi
echo $$ > "$PIDFILE"

# $1 = pwm file name
function pwmdisable()
{
	ENABLE=${1}_enable
	# No enable file? Just set to max
	if [ ! -f $ENABLE ]
	then
		echo $MAX > $1
		return 0
	fi

	# Try pwmN_enable=0
	echo 0 > $ENABLE 2> /dev/null
	if [ `cat $ENABLE` -eq 0 ]
	then
		# Success
		return 0
	fi

	# It didn't work, try pwmN_enable=1 pwmN=255
	echo 1 > $ENABLE 2> /dev/null
	echo $MAX > $1
	if [ `cat $ENABLE` -eq 1 -a `cat $1` -ge 190 ]
	then
		# Success
		return 0
	fi

	# Nothing worked
	echo "$ENABLE stuck to" `cat $ENABLE` >&2
	return 1
}

# $1 = pwm file name
function pwmenable()
{
	ENABLE=${1}_enable
	if [ -f $ENABLE ]
	then
		echo 1 > $ENABLE 2> /dev/null
		if [ $? -ne 0 ]
		then
			return 1
		fi
	fi
	echo $MAX > $1
}

function restorefans()
{
	local status=$1
	echo 'Aborting, restoring fans...'
	let fcvcount=0
	while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
	do
		pwmo=${AFCPWM[$fcvcount]}
		pwmdisable $pwmo
		let fcvcount=$fcvcount+1
	done
	echo 'Verify fans have returned to full speed'
	rm -f "$PIDFILE"
	exit $status
}

trap 'restorefans 0' SIGQUIT SIGTERM
trap 'restorefans 1' SIGHUP SIGINT

# main function
function UpdateFanSpeeds {
	let fcvcount=0
	while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
	do
		#hopefully shorter vars will improve readability:
		pwmo=${AFCPWM[$fcvcount]}
		tsens=${AFCTEMP[$fcvcount]}
		fan=${AFCFAN[$fcvcount]}
		let mint="${AFCMINTEMP[$fcvcount]}*1000"
		let maxt="${AFCMAXTEMP[$fcvcount]}*1000"
		minsa=${AFCMINSTART[$fcvcount]}
		minso=${AFCMINSTOP[$fcvcount]}
		minpwm=${AFCMINPWM[$fcvcount]}
		maxpwm=${AFCMAXPWM[$fcvcount]}

		read tval < ${tsens}
		if [ $? -ne 0 ]
		then
			echo "Error reading temperature from $DIR/$tsens"
			restorefans 1
		fi

		read pwmpval < ${pwmo}
		if [ $? -ne 0 ]
		then
			echo "Error reading PWM value from $DIR/$pwmo"
			restorefans 1
		fi

		# If fanspeed-sensor output shall be used, do it
		if [[ -n ${fan} ]]
		then
			read fanval < ${fan}
			if [ $? -ne 0 ]
			then
				echo "Error reading Fan value from $DIR/$fan"
				restorefans 1
			fi
		else
			fanval=1  # set it to a non zero value, so the rest of the script still works
		fi

		# debug info
		if [ "$DEBUG" != "" ]
		then
			echo "pwmo=$pwmo"
			echo "tsens=$tsens"
			echo "fan=$fan"
			echo "mint=$mint"
			echo "maxt=$maxt"
			echo "minsa=$minsa"
			echo "minso=$minso"
			echo "minpwm=$minpwm"
			echo "maxpwm=$maxpwm"
			echo "tval=$tval"
			echo "pwmpval=$pwmpval"
			echo "fanval=$fanval"
		fi

		if (( $tval <= $mint ))
		  then pwmval=$minpwm # below min temp, use defined min pwm
		elif (( $tval >= $maxt ))
		  then pwmval=$maxpwm # over max temp, use defined max pwm
		else
		  # calculate the new value from temperature and settings
		  pwmval="(${tval}-${mint})*(${maxpwm}-${minso})/(${maxt}-${mint})+${minso}"
		  if [ $pwmpval -eq 0 -o $fanval -eq 0 ]
		  then # if fan was stopped start it using a safe value
		  	echo $minsa > $pwmo
			# Sleep while still handling signals
			sleep 1 &
			wait $!
		  fi
		fi
		echo $pwmval > $pwmo # write new value to pwm output
		if [ $? -ne 0 ]
		then
			echo "Error writing PWM value to $DIR/$pwmo"
			restorefans 1
		fi
		if [ "$DEBUG" != "" ]
		then
			echo "new pwmval=$pwmval"
		fi
		let fcvcount=$fcvcount+1
	done
}

echo 'Enabling PWM on fans...'
let fcvcount=0
while (( $fcvcount < ${#AFCPWM[@]} )) # go through all pwm outputs
do
	pwmo=${AFCPWM[$fcvcount]}
	pwmenable $pwmo
	if [ $? -ne 0 ]
	then
		echo "Error enabling PWM on $DIR/$pwmo"
		restorefans 1
	fi
	let fcvcount=$fcvcount+1
done

echo 'Starting automatic fan control...'

# main loop calling the main function at specified intervals
while true
do
	UpdateFanSpeeds
	# Sleep while still handling signals
	sleep $INTERVAL &
	wait $!
done
The file at /etc/init.d/fancontrol looks like this:

Code: Select all

#!/bin/sh
#
# Fancontrol start script.
#

set -e

# Defaults
DAEMON=/usr/sbin/fancontrol
PIDFILE=/var/run/fancontrol.pid
PATH=/sbin:/bin:/usr/sbin:/usr/bin

test -f $DAEMON || exit 0

. /lib/lsb/init-functions


case "$1" in
        start)
                log_begin_msg "Starting fancontrol daemon..."
                start-stop-daemon --start -o -q -m -b -p $PIDFILE -x $DAEMON
                log_end_msg $?
                ;;
        stop)
                log_begin_msg "Stopping fancontrol daemon..."
                start-stop-daemon --stop -o -q -p $PIDFILE
                log_end_msg $?
                ;;
        force-reload|restart)
                sh $0 stop
                sh $0 start
                ;;
        *)
                log_success_msg "Usage: /etc/init.d/fancontrol {start|stop|restart|force-reload}"
                log_success_msg "  start - starts system-wide fancontrol service"
                log_success_msg "  stop  - stops system-wide fancontrol service"
                log_success_msg "  restart, force-reload - starts a new system-wide fancontrol service"
                exit 1
                ;;
esac

exit 0
I don't know how to get information on why the daemon wouldn't start... sorry

My system looks like this; it's a custom build:

Code: Select all

user@burrito-linux-desktop ~ $ inxi -Fc 0
System:    Host burrito-linux-desktop Kernel 3.0.0-12-generic x86_64 (64 bit) Desktop N/A Distro Linux Mint 12 Lisa
Machine:   Mobo ASUSTeK model M4A78LT-M version Rev X.0x Bios American Megatrends version 0802 date 08/24/2010
CPU:       Dual core AMD Phenom II X2 555 (-MCP-) cache 1024 KB flags (lm nx sse sse2 sse3 sse4a svm) 
           Clock Speeds: (1) 3200.00 MHz (2) 3200.00 MHz
Graphics:  Card: nVidia Device 1245 X.Org 1.10.4 driver nvidia Resolution 1280x1024@50.0hz 
           GLX Renderer GeForce GTS 450/PCI/SSE2 GLX Version 4.1.0 NVIDIA 280.13
Audio:     Card-1 ATI SBx00 Azalia (Intel HDA) driver HDA Intel Sound: ALSA v: 1.0.24
           Card-2 nVidia Device 0bee driver HDA Intel
Network:   Card Realtek RTL8111/8168B PCI Express Gigabit Ethernet controller driver r8169 
           IF: eth0 state: down speed: 10 Mbps duplex: half mac: [REDACTED]
Drives:    HDD Total Size: 496.1GB (36.2% used) 1: /dev/sda SAMSUNG_HM160HI 160.0GB 
           2: /dev/sdb WDC_WD3200BEVT 320.1GB 3: USB /dev/sdc STORAGE_DEVICE 16.0GB 
Partition: ID:/ size: 61G used: 29G (50%) fs: ext4 ID:/boot size: 92M used: 29M (34%) fs: ext4 
           ID:swap-1 size: 2.15GB used: 0.00GB (0%) fs: swap 
Sensors:   System Temperatures: cpu: 38.0C mobo: 39.0C gpu: 42C 
           Fan Speeds (in rpm): cpu: 3183 psu: 0 sys-1: 0 fan-2: 0 fan-3: 0 
Info:      Processes 164 Uptime 33 min Memory 845.4/3960.6MB Client Shell inxi 1.7.7
Any ideas? :|

Thanks in advance.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29614
Joined: Wed Jul 06, 2011 3:58 am

Re: Daemon won't start as a daemon - Fancontrol problems

Post by xenopeek »

Moved here by moderator
Image
Locked

Return to “Scripts & Bash”