Ubuntu – Autoexec.bat equivalents for Ubuntu that run with root privelidges

12.04bootSecurity

On Ubuntu there must be several "autoexec.bat" equivalents as I can see files like rc.local which seem to do very little because the comments in the file says it doesn't work by default and I tried putting this command in there and it didn't work.

What I need to know is where I place this line.

echo 0 | /usr/bin/tee -a /sys/class/leds/smc::kbd_backlight/brightness

I also saw a rc.local in /etc/init.d as well but where I would place the cmd to make it work is a mystery as there are two functions in there so do I place it at the end of one of those functions or at the last line?

The command needs root permission to run and ideally it should be executed when the computer starts. As the command dims the keyboard of my MacBook Air.

Also, I would also like to know what file I'd need to edit if only a specific user would have this command run. As its an root command and I don't want to give admin or sudo access to that user I would like this command to execute as root when the user logs in and the user cannot stop that command from running.


Additional Discoveries


$HOME/.profile is the login script but doesn't run as root, it works if you sudo the command but thats not want I need.

The /etc/profileis weird, it doesn't execute at boot but when I go into a shell and execute sudo -i" it runs as soon as the sudo goes into interactive mode


A neat short cut


With version 14.04, might have worked in earlier versions, you simply add a line to crontab using sudo crontab -e and add a line "@reboot whatevercommandwithfullpath" and it works. But with this new version I created a upstart script which is closer to how it should be done.

Best Answer

You can create a daemon witch is the right way to do it. However it's a little harder than rc.local.

see here : https://superuser.com/questions/530071/installing-daemon-on-a-fresh-ubuntu-system

detailed help (you can take example of /etc/init.d/hostname) :

copy the skeleton :

sudo cp /etc/init.d/skeleton /etc/init.d/keyboard_backlight

edit the skeleton :

sudo nano /etc/init.d/keyboard_backlight

search for this

do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
                || return 1
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
                $DAEMON_ARGS \
                || return 2
        # Add code here, if necessary, that waits for the process to be ready
        # to handle requests from services started subsequently which depend
        # on this one.  As a last resort, sleep for some time.

}

and change it like this :

do_start()
{
        echo 0 | /usr/bin/tee -a /sys/class/leds/smc::kbd_backlight/brightness
}

Comment out or delete the lines inside the do_stop and the do_reload

do_stop()
{
}
do_reload()
{
}

Save the file.

Give the execution permission to the file :

sudo chmod 755 /etc/init.d/keyboard_backlight

Test your service :

sudo /etc/init.d/keyboard_backlight start

If everything ok, load your service for startup

sudo update-rc.d keyboard_backlight defaults 99

Best regards.

Related Question