Ibuildings Blogs
Thursday, July 24. 2008Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
I found myself migrating a lot of bash scripts to python and php, they where growing in size and complexity and they were becoming very difficult to maintain.
I generally let my scripts 'evolve'.
The simplest of maintenance tasks usually start out as simple Bash shell script. Tasks that require more than a screen full of scripting code get split up in a Bash part (for non-logic stuff like logging and locking) and PHP stuff (for the 'meat' of the task), because when you're a PHP shop it's easier to maintain for everybody. Then some have evolved to require functions for code reuse. And a select few actually end up entire object oriented applications in and of themselves (though I can only think of one example right now). I guess the point is that you start as simple as possible. But I generally like (at least for small to medium tasks) to let the non-logic of a task be handled by the shell, which is better suited for the environment anyway.
Absolutely nothing.
But if all I want to execute is an rsync, or deleting some cache, or simple maintenance tasks, I generally don't like to make a PHP script for that. And besides proving to myself that I know a few basics about shell scripting (which I would think is pretty handy when you're writing software which is hosted on Unix platforms) I don't like the overhead of PHP for simple tasks. I have built some (fairly large) command line tools that are purely PHP, which is great for maintainability. And I definitely think PHP has a place on the command line. Though not necessarily in small maintenance scripts where I don't even want to care whether a PHP binary is on the machine.
Using files is racy and invites bugs.
Instead use directories or symlinks which are guaranteed to be created atomically. I.e rather than if [ ! -f file ]; then touch file; fi use if ! mkdir lockdir; then echo already locked; fail;fi
The script name can be detected like this
# Figure out the name of this script (without the path, etc) SCRIPTNAME="$(basename $0)" |