Clojure for system administration

759 views
Skip to first unread message

ajay gopalakrishnan

unread,
Feb 3, 2010, 4:50:23 PM2/3/10
to clo...@googlegroups.com
Hi,

I was wondering if the time is right to replace Perl/Bash/Ruby/Python with Clojure for system administration / dev productivity scripts. I would not do things in Java (for obvious reasons), but clearly Clojure is more concise than Java in most cases.
I would however want to know in detail if it is suitable for Sysadmin tasks like the following:
  1. Count # words in a folder , Count # files of particular extension in a folder or search regular expression
  2. Search for a particular regexp in a search path
  3. Copy files from one folder to another
  4. Walk through a directory structure
  5. Fork another program and read it's results
I've gone through this tutorial http://java.ociweb.com/mark/clojure/ , but please point me to clojure.contribs that will make these kind of tasks easier.

Thanks
Ajay

Travis

unread,
Feb 3, 2010, 6:06:54 PM2/3/10
to Clojure
I would imagine opening a clojure repl and loading the leiningen
library.

http://github.com/technomancy/leiningen/blob/94c675afe6767aa142bfc882298f0f828e476e24/README.md

Stuart Sierra

unread,
Feb 4, 2010, 11:33:43 AM2/4/10
to Clojure
Clojure can certainly do these things; clojure-contrib contains many
file and io-related utilities. But remember that Clojure, like any
Java program, takes more time to start up than "scripting" languages
like Perl/Bash/Ruby/Python, so it may be less suitable for programs
that you intend to run at the command-line.

-SS

On Feb 3, 4:50 pm, ajay gopalakrishnan <ajgop...@gmail.com> wrote:
> Hi,
>
> I was wondering if the time is right to replace Perl/Bash/Ruby/Python with
> Clojure for system administration / dev productivity scripts. I would not do
> things in Java (for obvious reasons), but clearly Clojure is more concise
> than Java in most cases.
> I would however want to know in detail if it is suitable for Sysadmin tasks
> like the following:
>

>    1. Count # words in a folder , Count # files of particular extension in a


>    folder or search regular expression

>    2. Search for a particular regexp in a search path
>    3. Copy files from one folder to another
>    4. Walk through a directory structure
>    5. Fork another program and read it's results
>
> I've gone through this tutorialhttp://java.ociweb.com/mark/clojure/, but

Phil Hagelberg

unread,
Feb 4, 2010, 12:35:52 PM2/4/10
to clo...@googlegroups.com
On Thu, Feb 4, 2010 at 8:33 AM, Stuart Sierra
<the.stua...@gmail.com> wrote:
> Clojure can certainly do these things; clojure-contrib contains many
> file and io-related utilities.  But remember that Clojure, like any
> Java program, takes more time to start up than "scripting" languages
> like Perl/Bash/Ruby/Python, so it may be less suitable for programs
> that you intend to run at the command-line.

Also relevant is the fact that launching Clojure from the command-line
is very inconvenient compared to scripting languages. If you want
something simple you can just put on your path, you'll need to wrap it
in a bash (or other language) script anyway to handle the classpath,
etc. The combination of startup time and the need to roll your own
bash script even for simple things has kept me from wanting to use
Clojure as a perlish-replacement.

-Phil

Richard Newman

unread,
Feb 4, 2010, 12:47:59 PM2/4/10
to clo...@googlegroups.com
> in a bash (or other language) script anyway to handle the classpath,

The classpath is a perpetual source of frustration. It drives me nuts
every time I have to restart swank to work on a different project with
a different classpath. It certainly means that something like a
Clojure shell would be unnaturally limited.

Brenton

unread,
Feb 4, 2010, 1:47:45 PM2/4/10
to Clojure
I have created a clj command on my system (using Ruby, http://gist.github.com/247899)
that will run a Clojure script or launch the REPL.

For example: "clj script.clj" or just "clj script" will run as script.
You may also pass args to the Clojure script like this "clj script
arg1 arg2". You may also pass arguments starting with "-X" through to
Java. I also have shorthands for other things like the arg -Xjmx is
converted to -Dcom.sun.management.jmxremote. Just typing clj at the
command line anywhere will start a REPL. I created this when I started
using Clojure to make it easier to work with and have been hacking on
it for a while. I add stuff all the time for my own convenience.

The basic idea is that you have a default classpath which is used when
you are not working within a project. If I execute clj within a
directory that contains a lib directory then it will put the jars in
lib on the classpath. If there is no lib directory then it will use
the default classpath. I explicitly set which jars are on the default
classpath but I think a better general purpose approach would be to
have a directory somewhere on your system that contains the default
jars. For example groovy has the .groovy/lib dir under ~ for this
purpose. This would allow you to easily add default jars without
modifying the clj script.

I also use a file named .cljrc.clj to configure the REPL. When the
script starts it will find the nearest parent dir that contains a file
named .cljrc.clj. When it finds this file it will run it and then
start the java process in that directory. I usually create a default
file in my root directory or ~ and one in each project. This file
usually contains an exit function to shutdown the REPL but you can
also configure the REPL here so that you don't have to do it each
time.

I created this before leiningen was available but I still use it and
the two work together very well. I also configure Emacs to use this as
my Clojure binary.

(setq swank-clojure-binary "clj")

What the others have said is still true. I only use Clojure for admin
stuff when the startup time is not a factor. But this clj script takes
away a lot of the pain of launching Clojure from the command line.

I hope this has more signal than noise.
Brenton

On Feb 4, 9:35 am, Phil Hagelberg <p...@hagelb.org> wrote:
> On Thu, Feb 4, 2010 at 8:33 AM, Stuart Sierra
>

Michael Wood

unread,
Feb 4, 2010, 2:45:23 PM2/4/10
to clo...@googlegroups.com
On 4 February 2010 20:47, Brenton <bash...@gmail.com> wrote:
> I have created a clj command on my system (using Ruby, http://gist.github.com/247899)
> that will run a Clojure script or launch the REPL.
>
> For example: "clj script.clj" or just "clj script" will run as script.
[...]

Clojure also ignores lines starting with #!, so you can have a file
with the first line being:

#!/usr/bin/env clj

and with the rest being just normal Clojure s-expressions, and then
you can even run it directly instead of "clj script.clj". :)

--
Michael Wood <esio...@gmail.com>

Tim Clemons

unread,
Feb 5, 2010, 12:42:14 AM2/5/10
to Clojure
Perhaps the solution is to have a *nix shell implemented in Clojure.
That would limit the start-up issue to a single initial instance.
Then the user can proceed to use regular command-line functionality
interspersed with Clojure scripts. Think of it as a hybrid REPL.

On Feb 4, 9:35 am, Phil Hagelberg <p...@hagelb.org> wrote:

> On Thu, Feb 4, 2010 at 8:33 AM, Stuart Sierra
>

Greg

unread,
Feb 5, 2010, 12:56:19 AM2/5/10
to clo...@googlegroups.com
A much easier solution is to go with a lisp designed for exactly the task of scripting.

I whole-heartedly recommend newLISP for this:

http://www.newlisp.org/

Clojure is excellent when you need a powerhouse LISP capable of great feats, but newLISP is far better suited for scripting tasks, it was designed for that.

Or you can implement a *nix shell in Clojure. :-p

- Greg

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

ataggart

unread,
Feb 5, 2010, 2:55:19 AM2/5/10
to Clojure

On Feb 4, 9:35 am, Phil Hagelberg <p...@hagelb.org> wrote:

> On Thu, Feb 4, 2010 at 8:33 AM, Stuart Sierra
>

> <the.stuart.sie...@gmail.com> wrote:
> > Clojure can certainly do these things; clojure-contrib contains many
> > file and io-related utilities.  But remember that Clojure, like any
> > Java program, takes more time to start up than "scripting" languages
> > like Perl/Bash/Ruby/Python, so it may be less suitable for programs
> > that you intend to run at the command-line.
>
> Also relevant is the fact that launching Clojure from the command-line
> is very inconvenient compared to scripting languages.

Does nailgun not solve that issue?

Chouser

unread,
Feb 5, 2010, 9:59:27 AM2/5/10
to clo...@googlegroups.com
On Thu, Feb 4, 2010 at 11:33 AM, Stuart Sierra
<the.stua...@gmail.com> wrote:
> Clojure can certainly do these things; clojure-contrib contains many
> file and io-related utilities.  But remember that Clojure, like any
> Java program, takes more time to start up than "scripting" languages
> like Perl/Bash/Ruby/Python, so it may be less suitable for programs
> that you intend to run at the command-line.

This has been the main reason I haven't completely abandoned
Python and ruby for such tasks. I think the original question
was phrased perfectly -- "wondering if the time is right". For
me, I'd say "not quite yet". I'm still holding out for
Clojure-in-clojure targeting a unix-embracing, fast-starting
host platform like golang or parrot.

--Chouser
http://joyofclojure.com

Meikel Brandmeyer

unread,
Feb 5, 2010, 10:01:50 AM2/5/10
to Clojure
Hi,

On Feb 5, 6:42 am, Tim Clemons <tclem...@gmail.com> wrote:

> Perhaps the solution is to have a *nix shell implemented in Clojure.
> That would limit the start-up issue to a single initial instance.
> Then the user can proceed to use regular command-line functionality
> interspersed with Clojure scripts.  Think of it as a hybrid REPL.

See http://www.scsh.net/

Sincerely
Meikel

e

unread,
Feb 5, 2010, 10:21:40 AM2/5/10
to clo...@googlegroups.com
On Fri, Feb 5, 2010 at 12:56 AM, Greg <gr...@kinostudios.com> wrote:
A much easier solution is to go with a lisp designed for exactly the task of scripting.


Woah!  Seems like an understatement.  This newLISP looks POWERFUL.  Lot's of new stuff to read.  Thank you, thank you.
 

Greg

unread,
Feb 5, 2010, 11:55:35 AM2/5/10
to clo...@googlegroups.com
Woah!  Seems like an understatement.  This newLISP looks POWERFUL.  Lot's of new stuff to read.  Thank you, thank you.

No problem!

Here are some more resources on it you might find useful:


Also, I recommend using the latest development version as there are some fairly significant changes between it and the stable one. The documentation is updated for the dev versions and I've never had any real stability issues with them.


The community is located here:


- Greg

Phil Hagelberg

unread,
Feb 5, 2010, 12:02:51 PM2/5/10
to clo...@googlegroups.com

Only about halfway; boot time issues are solved, but classpath issues
are not. In addition, you bring in a whole new "dirty VM" notion that
may or may not cause problems depending on how you use it. Plus you
have to decide how to start your new Clojure service--will it be for
the lifetime of the machine (in /etc/init.d) or just for the user's
login session? (in .profile then; but how to stop it?)

-Phil

Jeff Rose

unread,
Feb 5, 2010, 1:20:34 PM2/5/10
to Clojure

On Feb 5, 6:02 pm, Phil Hagelberg <p...@hagelb.org> wrote:

I've been wondering about this. The classpath issue seems like a
major thorn in the side of the JVM, especially for Clojure and other
more scripty languages. Besides, it makes it a lot more annoying to
just pop open a file in Vim or Emacs and start editing. Is there not
some way we can figure out to get around this? Even if it's a bit of
a hack, if it works it would be incredibly helpful to have a lib in
contrib that could help get around restarting the JVM all the time.
It especially sucks when you've got some useful state in the REPL that
you have to trash in order to load a new jar file or add something new
to the structure of a project. Could we use something like remove-ns
to cleanup the namespaces, and then load updated files, maybe even
symlinked into temporary names that are always on the classpath, or
something like that?

nchubrich

unread,
Feb 5, 2010, 1:54:09 PM2/5/10
to Clojure
Chouser---

The Parrot vm looks really great for Clojure----presumably Clojure
could have continuations and TCO there.

On Feb 5, 9:59 am, Chouser <chou...@gmail.com> wrote:
> On Thu, Feb 4, 2010 at 11:33 AM, Stuart Sierra
>

Greg

unread,
Feb 5, 2010, 2:02:18 PM2/5/10
to clo...@googlegroups.com
What about a Clojure directly on top of the LLVM?

It would be super-fast, you could start from scratch and leave behind all of the issues associated with the JVM, no more complicated classpath, namespace, imports, etc. and the startup time would be very fast.

I only wish I had the time to work on such a project...

- Greg

e

unread,
Feb 5, 2010, 9:56:04 PM2/5/10
to clo...@googlegroups.com
sounds like a good idea to me, too.

e

unread,
Feb 5, 2010, 10:00:05 PM2/5/10
to clo...@googlegroups.com
along these lines, it seems like this (see link below) was supposed to be included, but I'm not sure.  It combines bash with newLISP.  for those wanting to do this with clojure, this could be a good model.  That is, it might be instructive to check out the newLISP progress, even if clj is your primary destination:


With this, regular bash works as normal, but if you start with '(' ... then newLISP kicks in.  That's simpler than doing:
 (exec "ls -lrt")

... for example.

Jeff Schwab

unread,
Feb 6, 2010, 10:01:34 AM2/6/10
to clo...@googlegroups.com

Asbjørn Bjørnstad

unread,
Feb 8, 2010, 4:51:20 AM2/8/10
to Clojure
On Feb 5, 12:33 am, Stuart Sierra <the.stuart.sie...@gmail.com> wrote:
> Clojure can certainly do these things; clojure-contrib contains many
> file and io-related utilities.  But remember that Clojure, like any
> Java program, takes more time to start up than "scripting" languages
> like Perl/Bash/Ruby/Python, so it may be less suitable for programs
> that you intend to run at the command-line.

Scripting languages also have very easy access to launch/use other
utilities.

What I did find clojure useful for back when I had time to look at it
(a year ago), was presentation of data. For example, I had one script
that scanned the maillog and made charts (Using Jfreechart) of sent/
received/rejected emails, another took patch data from all our servers
and gave a historical view of how many servers were patched/unpatched/
had missing data. This is things where there is no advantage to
scripting languages, and availability of java libraries is an
advantage.
--
-asbjxrn

Reply all
Reply to author
Forward
0 new messages