This deserves some link love
Andy bogged a piece of advice that I have him which I got from Barry… and if you want to know how to get the true absolute path to the real location of the current script is from inside of it (like phps realpath and __FILE__) I suggest you check it out
As Close to A Real Daemon As Bash Scripts Get
I’ve written a little something which is gaining some traction internally, and I always intended to share it with the world. So… Here. daemon-functions.sh
What it does is allow you to write a bash function called “payload” like so:
function payload() {
while [ true ]; do
checkforterm
date
sleep 1
done
}
source path/to/daemon-functions.sh
Once you’ve done that it all just happens. daemon-functions gives you logging of stderr, stdout, a pid file, start, stop, pause, resume, and more functions. when you start your daemon it detaches completely from your terminal and runs in the background. Works very simply with monit straight out of the box. you can have as many daemons as you wish in the same directory and they wont clobber each other (as the pid, control, and log files all are dynamically keyed off of the original script name.) Furthermore inside your execution loop inside of the payload function place a checkforterm call at any place which it makes sense for your script to be paused, or stopped. it can detect stale pid files and run anyway if the process isnt really running. As an added bonus you dont actually have to loop inside payload, you can put any thing in there, have a script thats not a daemon, but will take an hour, day, week, month to finish? stick it in, run it, and forget it.
AIM Spam
has any body else noticed a huge increase in AIM spam lately?
I’m just waiting for the akismet-style service/plugin combo to come along for the IM space… It’s probably long overdue.
building sed for osx
I work in linux a lot… not bsd. So the OSX (bsd style) implementation of sed really throws me for a loop when I go text-file-spelunking, whats worse is that my scripts using sed aren’t portable between the two OSs.
A quick googling this morning landed me here: http://wiki.octave.org/wiki.pl?OctaveForMac which gives perfectly good instructions on installing sed. except it didnt work. I grabbed the latest version of sed (4.1.5) and got the error
sed: 1: "install_sh=/Users/apoka ...": command i expects \ followed by text sed: 1: "install_sh=/Users/apoka ...": command i expects \ followed by text
Ironic, huh? Well taking a guess that at some point sed hadcome to depend on its own functionality to configure itself I jumped back a version… Figuring i replace BSD sed with an out of date GNU sed, and then use the old GNU sed to build the new GNU sed. Which worked great. I Installed first sed-3.0.2, and then 4.1.5 in this manner:
./configure --prefix=/usr/ --with-included-regex --with-included-gettext && make && sudo make install
I’m happy with my -r again…
# date | sed -r s/'[0-9]‘/’?'/g Thu Apr ?? ??:??:?? PDT ????
I keep marking this as unread in google reader…
I keep marking this as unread in google reader so that Its there when I need it… which probably means I should just blog it… automating firefox via telnet
Daemonize Anything
I hacked together this little C program from this other little c program. Basically acts as an execution wrapper that lets you fork() and detach and run a command in the background with a pidfile and log file for program output. So far I havent had any problems with it… but then I’m not a true C guy so any input is welcomed.
-v is for verbose, damnit
So, the vast majority of every day administrative command line utilities for Linux use -v as the switch for verbose…. when you use -v you EXPECT verbose. Well sometimes you get that one package which just CANNOT follow the rules. Someone has to think outside the box, someone has to be a unique snowflake. That someone should not be a mass process killing utility! whoever thought up that the argument -v to pkill should INVERT THE MATCH should really take a long slow look at how important being unique really is… because if you’re not aware of this, and you run something like pkill -9 -v nagios…. as root… it’s not going to do what you expect. Nothing good comes of that command.
This has been a PSA
Logging post data
Lets say you have a relatively complex php web application, like wordpress. You have it running under apache (which is common.) You have good control of your site via .htaccess (which is common — permalinks and all.) And something happens to your blog (e.g. someone is exploiting some unknown vulnerability to compromise your content), which you want to track down. So you want to log, for instance, HTTP POST data. Your first instinct might be to add some logging code to index.php (mine was) But there are a lot of possible places which might be directly accessed, especially in the admin. So The trick I use (and this is probably the only time I’ve ever condoned this) is to use PHPs auto_prepend_file functionality.
Make a /home/user/postlog/ directory, then a /home/user/postlog/logs/ directory (and chmod a+rw that one.) Next make a simple /home/user/postlog/postlog.php file with the following contents:
<?php
if ( isset($_POST) && is_array($_POST) && count($_POST) > 0 ) {
$log_dir = dirname( __FILE__ ) . '/logs/';
$log_name = "posts-" . $_SERVER['REMOTE_ADDR'] . “-” . date(”Y-m-d-H”) . “.log”;
$log_entry = gmdate(’r') . “\t” . $_SERVER['REQUEST_URI'] . “\r\n” . serialize($_POST) . “\r\n\r\n”;
$fp=fopen( $log_dir . $log_name, ‘a’ );
fputs($fp, $log_entry);
fclose($fp); }
?>
Finally add this line to the top of your .htaccess file:
php_value auto_prepend_file /home/user/postlog/postlog.php
If all went well this should start logging any request to any php file with any post data into the /home/user/postlog/logs/ direcory (with a unique log per ip per day)
Autumn Leaves Leaf (Irc Bot) — Chanlogger
Simple, straight forward. Logs channel messages.
class Chanlogger < AutumnLeaf def did_receive_channel_message(sender, channel, msg) log_base = File::dirname(File::dirname(__FILE__)) + "/data/logs" subdir = log_base + "/" + Date::today.to_s[0..6] Dir::mkdir(subdir) if false == File::directory?(subdir) logfile = subdir + “/” + channel.to_s.sub(’#',”) + “@” + Date::today.to_s f = File::open(logfile, ‘a’) f.puts(Time::now.to_s + “\t” + sender.to_s + “\t” + msg.to_s) f.close() end end
Subscribe to the comments for this post

