nohup 2>&1 & WTF

Let me explain… As a relative command line newbie the characters 2>&1 & at the end of commands are quite intreguing. I figured out what they mean

The command line apps have 3 types of output

  • Standard input (STDIN)
  • Standard output (STDOUT)
  • Error output (STDERR)

So, when you run this command:

[tim@desktop]$ programName > filename.log 2>&1

The output of the program is output into the file and the output from output 2 (STDERR) is sent to output 1 (STDOUT). This effectively sends any errors into the file also.

If you add a space& like so to the end of the command it sends it to the background and returns the pid (in case you need to track/kill it).

[tim@desktop]$ programName > filename.log 2>&1 &

Thanks to: http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/comment-page-3/ for the detail (& Sasha for clarifying some things)

Whats the Procedure?

My first stored procedure in MySQL. I hope it doesnt open a can of worms for me. I’ve already experienced the pitfalls of putting too much logic in the database (SP’s) and don’t intend on allowing history to repeat itself.

CREATE PROCEDURE CreateReportUnits(IN num INT, unit VARCHAR(20))
SQL SECURITY INVOKER
BEGIN
 TRUNCATE test.report_days;
 SET @i = 0;
 WHILE @i < num DO
 SET @i = @i + 1;
 SET @query = CONCAT("INSERT INTO test.report_days ( report_day_timestamp ) VALUES ( DATE_SUB(CURDATE(),INTERVAL ",@i," ",unit,") );");
 PREPARE statement FROM @query;
 EXECUTE statement;
 END WHILE;

END

# To invoke use:
CALL CreateReportUnits(6 'MONTH');

The invocation will create a record for the last 6 months based on today’s date to use in reports.

Protected: Making time stand still

This post is password protected. To view it please enter your password below:


Pipe sed awk curl

Here’s a script I had to write at work for moving files into a backup directory if they’d been moved to S3. It was very interesting for me to write as it used the holy grail of bash “pipe, sed, && awk”!

#!/bin/bash
site=$1
file=$2;
# Establish the name of the file.
name=`basename "$file"`;
# Establish the numbered directory to backup into.
dir=`dirname "$file" | awk '{print $NF}' FS=\/`
# Form the backup location.
backup="/data/images/$site/backup/"$dir"/"
# Create the backup location (if it doesnt exist).
mkdir -p $backup;
# Check the file exists on S3
uri=`echo http://images.$site.com.au.s3.amazonaws.com$file | sed "s/ /%20/g"`

curl -I $uri 2>&1 | grep 'HTTP/1.1 200'
if [ $? = 0 ]; then
# More user feedback
echo "Moving "$file" into "$backup$name;
# Perform the move (@todo logging & deletion based on $mode).
mv "$file" "$backup$name";
else
echo "Warning: file doesnt exist on S3 ($uri)";
fi
echo ""; # A new line just to space the feedback out a bit.

SVN External

The command to add an external is:

  1. Navigate to the directory you want the external brought into.
  2. sudo svn propset svn:externals 'http://{path to repo}/ {name of dir}' .
  3. Commit then update to receive the external files.

Note the quotes around the {path to repo} and {name of dir}. Also note the dot at the end of the line to indicate the property setting on the current directory.

My initial playing with this proves changes made & committed in the externally included files will arrive in the external repo when it is updated. I’m impressed. With great power comes great responsibility – be warned.

PHP __LINE__ incorrect

I had some weirdness with the __LINE__ constant reporting the incorrect line. A colleague helped me get to the bottom of it.

There were too many line endings in the file. The only way to “see” them was in vi. They appeared as ^M characters.

The solution, replace them with nothing. I’m certain there are other ways, but this worked for me.

:%s/^M//g

To get the ^M you need to Ctrl+v then Ctrl+m.

Thanks superspace!

On the Third Day…

Resurrection of Deleted Revisions

If you manage to delete an entire folder and need to resurrect it from the repository, the solution is as follows. You must perform a URL to URL copy. The trick is ~ in the source URL, include the revision number the code last existed at. So to un-delete the repo/ folder at revision number 29600 from http://svn.domain.com/repo use this command

svn copy http://svn.domain.com/repo@29600 http://svn.domain.com

This copies revision 29600 into the destination path. Note: you dont need to include the destination folder name as it comes across with the copy.

Now for some background on how this happened. I’d been working on a project which required an svn:externals inclusion. So, during the devlopment (typically) the spec evolved and it turned out that the external wasn’t required any more. I needed to remove this relationship from the branch and proceded to svn delete it, and then svn commit. Perhaps I should have checked first, but it deleted the code from the external. repository location

I hindsight – I’d just delete the svn:externals property and move on…

SVN Eats Leaves & Branches

I spent the entirety of yesterday untangling a mess of subversion branches. What had happened was lots of (3) development branches had been merged into trunk, but not back out into the other branches. This caused some changes to be mising when the branches were merged back into trunk. This happened for 3 different sites repositories.

I’d like a mechanism for alerting or automatically reverse merging from trunk into the active development branches. This “hook” would:

  • Alert admins trunk has been updated.
  • Perform the reverse merge to active dev branches (provided there are no conflicts).
  • Possibly perform fresh export the dev branches after merge?
  • Perform a fresh export of trunk to the server for UAT.

This would avoid the situation in future. It has been an interesting excercise which I will not be doing again! Lesson learned.

Ableton Live on Linux

http://dasacc22.wordpress.com/2008/08/27/ableton-live-linux-and-wine/

I live in hope.

MySQL Utility Statements

Here are a few MySQL commands I used recently which were quite handy.

FLUSH TABLES {dbName}.{tableName};

This one clears the query cache for the specified table.

SHOW TABLE STATUS FROM {dbName} LIKE '{tableName}';

This one shows useful data such as Auto increment, length and modified time of a table.

SHOW TABLES FROM {dbName} LIKE '%{tableName}%';

This lists the tables matching the criteria in the LIKE clause.