Home » 2013 » April

Archive for April, 2013

I was sitting at home reading up on my pets. I have a pair of Sugar Gliders, Icarus and Finn, and they fascinate me. They have an apparent intelligence that intrigues me the most, so I’m trying to figure out effective ways of teaching or measuring them. I want something that is less anthropomorphic than “Ooooh aren’t you smart?”

sugargliders

That brings me to the Arduino. Specifically, I have a FreakLabs HackerMoms FredBoard and an enclusure, and I am very happy with it. I’ve spent the last few days playing around with it more, getting used to collecting, processing, and displaying sensor data. A frozen liquor bottle and a cup of hot tea helped get a thermal sensor working, and the teapot provided a useful reference weight for a pressure sensitive foam.

 

I’m sure you can see where I’m going here. I figure I want to try and collect data from my pets, and see what interesting things I find. I’ve done some reading and thinking, and I’ve decided on the following:

 

  • Measuring the average use of the running wheel per night, and an attempt to calculate the distance “run” each night.
  • Measuring the amount of time spent in their nest each night.
  • Recording sounds
  • Recording video
  • Measuring foraging behaviour (I want to build a toy with holes, with sensors, to tell me how frequently they search through it.)

 

I also want to build a small foraging toy, and “label” the holes with a colour or shape, and see what happens when one shape is always loaded, half loaded, never loaded, etc. I have a few ideas on how they might react, perhaps they will become habituated to ignoring certain shapes and attending to others, or if they always check all their options…

More to come in the future.

 

See you next week.

-weetabix

 

 

Be the first to comment

Fie diddle dee dee, init, and init.d

Posted April 8, 2013 By weetabix

 

On being lazy:

 

I love being lazy. In fact, it’s fair to say I take it quite seriously.

One of my favorite teachers taught me something deeply memorable one day. He taught me that being lazy is the best way to approach any job. Not just for you, but for the job itself and everyone else it affects.

Now, on the face of it this might sound like some poor advice.

Follow me along here:

 

1. The goal of being lazy is to not do more work than you have to.

2. Doing something once is less work than doing it twice.

3. To do something only once, you have to do it right the first time.

So, therefore, if you want to be lazy, you have to do the job right the first time.

– From the unforgettable Chris Venne.

 

One of the best ways to be lazy is with init scripts. Since I’m a Xubuntu/Debian fanboy, I use both upstart init and (x)init.d style scripts. Now, these can be easy, and sometimes they can be hard, especially when you don’t have useful examples. (and especially more so when half the sites reap off each other and have the same ding dong mistake. More on that later.)

To avoid confusion, when I say upstart script, I mean the newer style scripts run from /etc/init/ by Upstart. When I say init.d or init script, I mean a script run by init from /etc/init.d/. Hopefully this helps avoid confusion. (Though in the future after init is very deprecated, I will likely just call then init scripts.)

Some people reading this likely know what upstart and init/init.d scripts are. If you do not, the short version is, they are scripts run by services (called Upstart and init) that manage other services. This is how you get stuff to start and stop on boot and shutdown.

I keep around some template files, so I never have to worry too much about remembering how to do it.

 

For init we have: (Remember, you can click on the top blue line to download the file.)

/weetabytes/blob/master/initscript
#!/bin/sh
#
 
### BEGIN INIT INFO
# Provides:          <nameofservice>
# Required-Start:    $local_fs $network $remote_fs
# Required-Stop:     $local_fs $network $remote_fs
# Should-Start:      NetworkManager
# Should-Stop:       NetworkManager
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Durr
# Description:       Herp a Durr
#
#
### END INIT INFO
 
DAEMON=<Binary to run>
#This is optional. It is used to load variables for this file.
SETTINGS=/etc/default/<same as initfilename>
 
([ -x $DAEMON ] && [ -r $SETTINGS ]) || exit 0
 
DESC="<What are you starting or stopping>"
DEFOPTS="<options to append to binary call>"
#this script is intended to start python binaries.
PYTHONEXEC="<path to python exec>"
PIDFILE=/var/run/<somedir>
#Set this to true is you decide to require a defaults file.
SETTINGS_LOADED=FALSE
 
# these are only accepted from the settings file
unset USER CONFIG HOST PORT EXTRAOPTS
 
. /lib/lsb/init-functions
 
check_retval() {
	if [ $? -eq 0 ]; then
		log_end_msg 0
		return 0
	else
		log_end_msg 1
		exit 1
	fi
}
 
is_running() {
	# returns 0 when running, 1 otherwise
    #remove pythonexec if you are calling the binary directly.
    PID="$(pgrep -f -x -u ${USER%:*} "$PYTHONEXEC $DAEMON $DEFOPTS.*")"
	RET=$?
	[ $RET -gt 1 ] && exit 1 || return $RET
}
 
load_settings() {
	if [ $SETTINGS_LOADED != "TRUE" ]; then
		. $SETTINGS
 
		[ -z "$USER" ] && {
			log_warning_msg "$DESC: not configured, aborting. See $SETTINGS";
			return 1; }
		[ -z "${USER%:*}" ] && exit 1
 
		OPTIONS="$DEFOPTS"
		[ -n "$CONFIG" ] && OPTIONS="$OPTIONS --config-file $CONFIG"
		[ -n "$HOST" ] && SERVER="$HOST" || SERVER=
		[ -n "$PORT" ] && SERVER="$SERVER:$PORT"
		[ -n "$SERVER" ] && OPTIONS="$OPTIONS --server $SERVER"
		[ -n "$EXTRAOPTS" ] && OPTIONS="$OPTIONS $EXTRAOPTS"
		SETTINGS_LOADED=TRUE
	fi
	return 0
}
 
start_bin() {
	load_settings || exit 0
	if ! is_running; then
		log_daemon_msg "Starting $DESC"
		start-stop-daemon --quiet --chuid $USER --start --exec $DAEMON -- $OPTIONS
		check_retval
		# create a pidfile; we don't use it but some monitoring app likes to have one
		[ -w $(dirname $PIDFILE) ] && \
        #remove pythonexec if you are calling the binary directly.
			pgrep -f -x -n -u ${USER%:*} "$PYTHONEXEC $DAEMON $OPTIONS" > $PIDFILE
	else
		log_success_msg "$DESC: already running (pid $PID)"
	fi
}
 
stop_bin() {
	load_settings || exit 0
	if is_running; then
		TMPFILE="$(mktemp --tmpdir <binary name>.XXXXXXXXXX)"
		[ $? -eq 0 ] || exit 1
		trap '[ -f "$TMPFILE" ] && rm -f "$TMPFILE"' EXIT
		echo "$PID" > "$TMPFILE"
		log_daemon_msg "Stopping $DESC"
		start-stop-daemon --stop --user ${USER%:*} --pidfile "$TMPFILE" --retry 30
		check_retval
	else
		log_success_msg "$DESC: not running"
	fi
	[ -f $PIDFILE ] && rm -f $PIDFILE
}
 
case "$1" in
	start)
		start_bin
	;;
	stop)
		stop_bin
	;;
	force-reload|restart)
		stop_bin
		start_bin
	;;
	status)
		load_settings || exit 4
		if is_running; then
			log_success_msg "$DESC: running (pid $PID)"
		else
			log_success_msg "$DESC: not running"
			[ -f $PIDFILE ] && exit 1 || exit 3
		fi
	;;
	*)
		log_failure_msg "Usage: $0 {start|stop|restart|force-reload|status}"
		exit 3
	;;
esac
 
exit 0

 

And for upstart we have two flavours:

 

The “run a simple binary or script” type:

/weetabytes/blob/master/upstart.conf
# <name of daemon>
#
#
# <Longer description>
#
#
 
description "Derp"
author "Herp Herp D Derp"
 
# The below "start on" line controls when it starts up. There are many options available.
start on (local-filesystems and net-device-up)
#Ditto for stop on.
stop on runlevel [016]
 
respawn
respawn limit 5 30
 
env uid=<user to run as>
env gid=<group to run as>
env umask=000
 
# This next line gets called on start, and killed on stop. Upstart tracks the running invocation automatically.
# I use the below exec line, but you can use just about anything.
 
exec start-stop-daemon -S -c $uid:$gid -k $umask -x <path/to/executable/> -- -d -l <log/me/here.log> -L warning
#exec /usr/bin/binary

 

And the “run some checks and scripting, before running the daemon/binary” type:

/weetabytes/blob/master/upstartscripted.conf
# <name of daemon>
#
#
# <Longer description>
#
#
 
description "Derp"
author "Herp Herp D Derp"
 
# The below "start on" line controls when it starts up. There are many options available.
start on (local-filesystems and net-device-up)
#Ditto for stop on.
stop on runlevel [016]
 
respawn
respawn limit 5 30
 
env uid=<user to run as>
env gid=<group to run as>
env umask=000
#A more insane start condition. This one is based off starting lightdm
start on ((filesystem
           and runlevel [!06]
           and started dbus
           and (drm-device-added card0 PRIMARY_DEVICE_FOR_DISPLAY=1
                or stopped udev-fallback-graphics))
          or runlevel PREVLEVEL=S)
 
stop on runlevel [016]
 
 
#The below lines are run as a shell script on start, and the started binaries killed on stop.
script
	if [ "$RUNLEVEL" = S -o "$RUNLEVEL" = 1 ]
	then
	    # Single-user mode
	    exit 0
	fi
    exec <some binary>
end script
 
#This runs after stop
post-stop script
    rm /some/tmp/files/
end script

 

Now, before, I mentioned some common problems with these. Permissions and environmental options are often the place to start. Using start-stop-daemon you have user selection with --chuid and --chgid. This works with both init and Upstart.

Typically, the simplest troubleshooting step is to run each line of the script, or if you suspect your flow control (IF, ELSE, etc), stuff the lines up in a script of their own, with echo output and sleeps to identify and temporally separate steps.

Sometimes, calling an external bash script can be useful as well. The binaries spawned by that script will still be watched by Upstart for the purpose of stopping them. With init, this is more difficult.

One last thing to remember, both init and Upstart scripts are interpreted as sh, or shell, not bash. Look for bashisms in your script, and if you cannot live without them, call an external script, ensuring it is executed by bash. (A bashism is a procedure or command specific to bash, and not found in other shells, such as sh and ksh.)

See you next week.

-weetabix

Be the first to comment

Bash tricks

Posted April 2, 2013 By weetabix

These are a collection of my Bash tricks, and snippets from my .bash_history and .bashrc. I hope they come in handy for you.

 

There is, of course, ctrl+r that allows you to do a reverse search through your bash history. Type to match, then hit enter to put it on the command line.

 

The history command allows you to modify your history, or just edit .bash_history.

 

To add a proxy to a CLI command:

http_proxy='http://192.168.0.1:80' wget http://ftp.somesite.net/pub/file.tar.gz

 

To find files of the form <filename>(1).flac, such as duplicate music files. (substitute .mp3 for .flac if you need)

find </in/this/path/> -regex .*([0-9]+)\.flac -exec echo {} +

 

!! reruns the last command issued, !<string> runs the last command that started with <string>, and pwd shows you where you are.

 

When you are affecting groups of files, you can use a regex, such as vi file[1234].txt to open file1.txt, file2,txt, etc. Of course, in vi, you need to use :n to move to the next file.

 

A command like rm file[1234].txt or rm file[1-4] will delete all file* that end in 1, 2 ,3 4. You can also do rm file[1-4]+ to catch file12 and file33. The “+” means match one or more “[1234]’s.”

This is similar to how the rename command works, though it uses perl regex instead.

 

Finally, one brain buster:

If I do more somefile and I intended to (or then need to) do vi somefile, in bash, you can do ^more^vi^ to make a substitution on the previous command only, to run vi somefile/

 

The command works in the syntax of ^find^replace^

 
Edited Apr 2nd. Thanks anna-.
Till next week,
-weetabix

Be the first to comment