Ubuntu startup script that controls an Arduino chip

I’ve got an Arduino that controls a lamp in my bedroom that goes off at 5:30 (I don’t get up at that ungodly hour by choice) weekdays and different times on weekends. This helps me get up and out of bed a bit more naturally. I won’t go into how it works (maybe a topic for another blog post) but I thought I’d share how to create a startup script for Ubuntu.

Before I wrote this script, every time I restarted the server, I’d have to open a console window and fire off the script, which is less than ideal.

Writing a script for Ubuntu for startup is a bit more complicated than just having what I had before:

#!/bin/nbash
cat /dev/ttyACM0

What I have now is three scripts. The two “main” scripts are the start cat and end cat scripts:

start_cat.sh

#!/bin/bash

# This script should be run as root, or it can be run as any user if the device is already
# readable/writable by any user (e.g. you've already done chmod 777 {device}

# Do we want to perform a chmod
chmod=true

# Your device location
device="/dev/ttyACM0"

# Get thc current directory of this script
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

log="${dir}/../storage/logs/scripts.log"

# Set a a timestamp variable
now=$(date +"%d-%m-%Y %H:%M:%S")

# Write a message to the log file - useful for debugging
# Comment this out if you don't want to do this
echo "[${now}] - Cat script starting. Attempting to watching device ${device}" >> $log

if [ $chmod == true ]
then
    chmodResult=$(chmod 777 ${device})
    if ! $chmodResult
    then
        echo "[${now}] - Failed to chmod device" >> $log
    fi
fi

echo "[${now}] = Starting cat" >> $log cat $device

What this script will do is chmod the device (/dev/ttyACM0) to 777, then constantly cat the device so when my PHP script sends the command to the Ardiuno, it sends the changes to the relay.

end_cat.sh

#!/bin/bash

# Get thc current directory of this script
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

log="${dir}/../storage/logs/scripts.log"

if [ "$(id -u)" != "0" ]; then
 echo "This script must be run as root" 1>&2
 echo "This script must be run as root" >> $log
 exit 1
fi

# Find the process and then kill it
# The braces ([ and ]) around cat means it’ll only return the actual script running, not the grep as well
kill $(ps aux | grep '[c]at /dev/ttyACM0' | awk '{print $2}')

Now, you’ll need to create a file in /etc/init.d. I’ve called it start_cat. Probably should call it something like light_control or at least something a bit more descriptive

start_cat

#!/bin/bash
### BEGIN INIT INFO
# Provides: cat_light
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: cat_light
# Description: This file cats /dev/ttyACM0 so that the PHP script can control the light relay on the ardiuno chip
#
### END INIT INFO

# The nohup command means that this script wont wait for the start_cat to finish - it will never finish unless
# the kill command is issued

case "$1" in
 start)

nohup /var/www/control/scripts/start_cat.sh > /dev/null 2>&1 & ;; stop) /var/www/control/scripts/end_cat.sh sleep 10 ;; restart) /var/www/control/scripts/end_cat.sh sleep 10 nohup /var/www/control/scripts/start_cat.sh > /dev/null 2>&1 & ;; *) echo "Usage: cat_light {start|stop|restart}" >&2 exit 3 ;; esac

Now, test that the start_cat command works by running ./start_cat start, ./start_cat stop and ./start_cat restart. Check the log file to make sure everything is working as expected.

Then runsudo update-rc.d start_cat defaults, reboot your machine and done! Your script will now startup when your machine boots.