Ubuntu – Sudo command in bash script halts the script

bashcommand linescripts

I have an early 2009 iMac with a sensor problem. The ODD and CPU fans run at full speed. To get them down to minimum speed in Ubuntu I need to set the speeds in the Terminal, using this method. Rather than do this after every boot, I made a bash script to do it for me, but it doesn't work.

This is my script:

#!/bin/bash
echo "Beginning script..."
echo "Logging in as root..."
sudo su
cd /sys/devices/platform/applesmc.768/
echo "Enabling manual override for Fan 1 (ODD fan)..."
echo '1' > fan1_manual
echo "Setting speed of Fan 1 (ODD fan) to 1650 RPM..."
echo '1650' > fan1_output
echo "Enabling manual override for Fan 3 (CPU fan)..."
echo '1' > fan3_manual
echo "Setting speed of Fan 3 (CPU fan) to 1200 RPM..."
echo '1200' > fan3_output
echo "Done."

The problem is at "sudo su". It prompts for a password, but after entering the password, nothing happens. It just halts.

Here is a screenshot

Best Answer

Your script halts because sudo su starts a new shell process. Your original shell process - the one that is running your script - is waiting for the sub-shell to end.

I propose to run whole script with root privileges instead of asking to log in as root from script itself. Remove sudo su part from your code and run script like that:

$ sudo ./yourscript

This is common practice. Note, that system administration scripts like for example update-grub don't ask for root password - they are just expected to be run as root.