#! Goodness - access CommandBox Commands, Recipes and OS commands

I’m playing around with the “#!/usr/bin/env box” scripts on Linux. I’ve been able to run only CFML so far. Putting any other command generates Lucee exceptions.
Is it possible to run OS native commands (e.g. “ping”, “mkdir”), Box Commands or Box Recipes from within the Box hash bang scripts?

HI, good question. The way shebang scripts are currently implemented, they are just stock execution of a CFML template and they have no extras added in. In fact, they completely bypass the rest of CommandBox. So, everything you mentioned is technically possible, but not easily since you’d have to reproduce it all yourself.

There’s a couple other options. The first is use the box exec command to run your scripts which runs them inside of CommandBox and gives them (indirect) access to all that stuff.

$> box exec myScript.cfm

Even better, would be to use the new task runner functionality in CommandBox 3.7 which lets you arbitrarily run any simple CFC file with all the goodness of a custom command mixed in including calling other commands, user interactivity, and ANSI formatted output.

$> box task run

https://ortus.gitbooks.io/commandbox-documentation/content/task-runners.html

Or if you don’t really need to run ad-hoc CFML and would be fine just executing some built in commands, you can just create a recipe and call it like this:

$> recipe myScript.boxr

https://ortus.gitbooks.io/commandbox-documentation/content/usage/execution/recipes.html

I realize none of those are quite the same as a shebang script, but they give you a lot more features. Maybe in the future, we can rework shebang scripts to run inside the CommandBox framework. I don’t believe there’s a technical reason why it doesn’t work that way.

Thanks!

~Brad

ColdBox/CommandBox Developer Advocate
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Hi Brad!

Thank you for your in-depth response, indeed I was a bit confused about the differences between commands, recipes, shebang scripts and the latest addition to CommandBox - tasks.

I was wondering if there’s a way to create a Linux service/daemon using CommandBox (hence my initial question about bash scripts). In my desired use case, it would start with the OS and even get restarted by the OS when it dies. It would be controlled by “service name start|stop|restart” commands. It would have access to the OS commands (ping, mkdir, etc.) as well as to CF commands (for example cfquery, cfftp). I have used shell scripts with inittab or systemd, but not sure I can include CommandBox in the equation. I am having difficulties making the first step - would an .sh script executing “box …something” be a good/possible idea? Could the “something” be a CommandBox task (as in your “box task run” example), doing its job and sleeping alternately, in an endless loop? Maybe you already have some examples that would help me get started.

I’d appreciate your insights. Thanks!

Yes, you can do that. Here’s a blog post that shows how to create an init.d script to start a CF server when your server boots.

http://pi.bradwood.com/blog/starting-the-cfml-server-on-boot

You can swap out the “server start” command with whatever you want, as long as it runs indefinitely. If you wanted to be able to “stop” it as a service as well, you’d need to get a little clever and write a PID file somewhere so you could kill it later, etc. This is what most init.d scripts do.

Thanks!

~Brad

ColdBox/CommandBox Developer Advocate
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Wow! This is exactly what I needed, thanks!