Skip to content
Jul 22 / admin

Need cron to run faster than 1 minute? Create a daemon in 60 seconds with shell scripts

The Problem

I recently encountered a problem in which files had to be transferred from a remote server to a local filesystem almost instantaneously. Initially, I created a shell script that would copy the files from remote to local. Then like a good novice sysadmin, I created a cron job to run every minute that triggered the script. It worked well enough, however I soon received complaints that the process was not instantaneous enough. Now if you’re wondering what possibly needs to be faster than one minute, to be fair, my client was printing labels, so waiting around for one minute for a label when they had hundreds to print in a day was frustrating for them.

The Solution

First I was asking my colleagues if there was a way to make cron run faster than one minute. However, that just seemed problematic, for if it ran too frequently, there could be a race condition. Ultimately, I decided that making a shell script that ran in an infinite loop would work perfectly. Of course I would needs a way to ensure the script was always running, and for that I would use cron. If cron realized the script was not running, it could initiate it. This would work well both if the script happened to stop, and if the server was rebooted for any reason.

Here’s the basics.

filename: /home/jeff/bin/bash_daemon.sh

#!/bin/bash

while [ 1 -eq 1 ]; do
# do some stuff
sleep 5
done

The basic premise is the loop. I added a five second wait, just to be somewhat courteous to the system, but you can set that to whatever you desire. Where it says #do some stuff, is where you put your logic.

filename: /home/jeff/bin/bash_cron.sh

#!/bin/bash

OUTPUT=`ps -ef | grep ‘[b]ash_daemon\.sh’`
if [ “$OUTPUT” ]
then
echo running
else
/home/jeff/bin/bash_daemon.sh
fi

This snippet checks to to see whether or not the process is currently running, if it is not, it starts it back up again. Notice the regular expression in the `grep` command, that stops `ps` from finding `grep` in the process list, and returns only the running script.

And of course don’t forget to edit your crontab. `su` to the user you will be running the script as and edit your crontab via `crontab -e`:

* * * * * /home/jeff/bin/bash_cron.sh > /dev/null 2>&1

That entry will run the script that checks that monitors your other script every minute. At least if it fails, it will be rebooted in a minute, but it’s unlikely it will ever fail. This is really useful to ensure that the script starts at bootstrap, which you could also accomplish by adding the daemon script to your startup scripts directory.

And there you have it, an seemingly instantaneous bash daemon.

Jan 6 / admin

Seven Things You Didn’t Know About Me

I’ve been tagged by Laura Thomson!

1. My first programs were written in BASICA, although at some point I think I messed around with Apple languages as well and quickly forgot. I wrote choose-your-own-adventure games at 10 years old. With no formal training at this age, at some point I realized the logic should be separate from the text, so I separated out into my own markup language that held information about color and position within the program.

2. I once cracked the code to the Nintendo game Adventures of Lolo by beating 30 levels and cracking the code with pencil and paper; that was long before I started drinking and lost my capacity for logic.

3. I once ran a large MP3 download site for the band Phish called phishphiles.org on a Red Hat box in my dorm room at UMASS: Amherst in 1999. I was approached by Rolling Stone magazine for download statistics which were subsequently published in the magazine.

4. I left college after one year to create my first PHP shopping cart application to run my business DividedSky.com which sold T-Shirts for jam bands. I ran it for 4 years while living in my office on the floor.

5. I used to have a band in high school called Ambient North. I thought we rocked. I got started in web design building web sites for my band. I used to make a new web site every month. Good practice.

6. I recently bought an electronic drum set and now I’m wondering why I ever wasted so much time writing songs and playing guitar. Go Roland V-Drums! I already pretty good at them since I’ve been visualizing playing them my whole life. ;-)

7. I just moved to Boston from the US Virgin Islands and everyone asks my why I did it. I never have a good answer for this question. Did I mention I’m currently freezing my ass off?

Tagees:

Nate Abele – friend and CakePHP Lead Developer
Lukas Karlsson – great friend and Manager of IT at The Broad Institute
Matt Langer – my friend who got me hooked on PHP on now makes fun of me because he’s a Pythonista
Mariano Iglesias – the unstoppable Argentinean
Felix Geisendorfer – the man, the myth, the German
Andy Dawson – fellow CakePHPer

And here are the rules:

  • Link your original tagger(s), and list these rules on your blog.
  • Share seven facts about yourself in the post—some random, some weird.
  • Tag seven people at the end of your post by leaving their names and the links to their blogs.
  • Let them know they’ve been tagged by leaving a comment on their blogs and/or Twitter.
Dec 11 / admin

GitHub: Because It’s Bitchin’

Since all my brethren over in CakePHP land have migrated to GitHub in one way or another, I have decided to do the same. I’m not yet fully aware of all of the power of collaborating with GitHub, but I do understand that have the ability to commit locally via GIT is awesome, other people forking your code, making changes, and merging also seems to facilitate sharing.

Both my CakePHP ACL Plugin and the jQuery/CakePHP Live Validation Plugin that Marc and I designed can both be found on GitHub. http://github.com/phishy

Sep 30 / admin

Live Validation with CakePHP and jQuery

Marc Grabanski (1marc) has been trying to spearhead the inclusion of a jQuery Helper in the CakePHP project for some time. I know what he really wanted was some validation of forms against the defined logic in his models. So while he was staying at my house when he came to Boston for jQuery Camp and The Ajax Experience, we stayed up until 3:30am coding this puppy. Here’s the demo: http://jeff.loiselles.com/projects/cake/live-validation/

Mar 3 / admin

CakePHP VersionBehavior on CakeForge

I decided to move my VersionBehavior from my own repository to CakeForge. This will help the advancement of the project significantly. I can now create and respond to tickets, forums, and post documentation. Come on over, hack on my nasty code, and give me some feedback.

http://cakeforge.org/projects/version/

Feb 11 / admin

VersionBehavior Slides from CakeFest 2008

I finally made my presentational debut at CakeFest 2008. I spoke about my newest incantation of a CakePHP VersionBehavior. Here are the slides, let me know if you have any questions.

VersionBehavior.pdf

Feb 9 / admin

Happy Faces @ CakeFest 2008

DSC02550.JPG

A picture is worth a thousand words, and I’m too tired to write..

Nov 11 / admin

CakePHP ACL Management Plugin

Blessed be the ACL behavior which is based upon the Tree behavior in CakePHP. Those two things singlehandedly make CakePHP worth every cycle.

Ever since I got AuthComponent and Acl working in my latest CakePHP application, managing the ACLs has been a bitch. For a while the ACL shell was broken so I was actually writing the SQL manually to manage permissions. I know of course there’s a component that could have made it easier for me, but I didnt doing that was any faster for some reason. Nonetheless..

I decided to man up and create my first plugin, please welcome, the ACL management plugin. I know this has been done before, but I haven’t seen anything lately that works with CakePHP 1.2. Plus, I used AJAX to make working with trees easier and faster. This way I dont have to load the whole tree on startup.

Everyone is more than welcome to share in the fun.

Acl Plugin Demo

And of course you can always get the latest source from GitHub.

http://github.com/phishy/acl_plugin/tree/master

Sep 18 / admin

CakePHP Live Podcast

Over in CakePHP-land, we have to decided to storm the bandwaves with our humorous commentary and technical discussions. Come listen to me say “Awesome! Cool! and Great!” a lot. You can download our pilot broadcast or you can tune in on September 18, 2007 12:00PM EST and join us on Freenode #cakephp-live to ask questions that will be answered on the air.

Welcome To The Show.
http://live.cakephp.org

Aug 24 / admin

CakePHP Versioning/History Behavior

Strangely enough, every time Core CakePHP Developer Nate Abele and I were standing near a train, we somehow got talking about creating a versioning behavior for CakePHP. Today, I was taking a train back from Boston to Connecticut where I live, so I decided to draft up an implementation while I was taking the two hour scenic train ride; it’s only fitting!

Sometimes it’s useful to keep history of a database table. Once you track the history of a table, it is trivial to do diffs on the records, or even revert to a previous revision. Implementing this is usually a manual process for each table unless you’re clever enough to create your own magic system. But luckily, with the extensible magic of CakePHP, I was able to draft up a behavior that can easily be added to any table in my database. Voila! Automatic versioning, diffing, and undo for any table in my schema. I’m still finishing up the unit tests and adding some features, but I will post my work soon. Are you interested?