Fork me on GitHub

What is the most "babashka" way to do,

$ kill -9 `ps -ef|grep spring.main.banner|grep -v grep|cut -c9-17


This seems to do the trick, but it is not winning in the readability category.... IMHO

(apply shell/sh (concat ["kill" "-9"] (map #(.trim %) (map #(subs % 9 17) (filter #(str/includes? % "spring.main.banner") (str/split (:out (shell/sh "ps" "-ef")) #"\n"))))))

Bob B02:07:39

like a lot of clojure code I write, if I go that long, I'd probably use threading (I swapped out some parts to match a repro I could spin up quickly):

$ bb '(->> (:out (shell/sh "ps" "-ef"))
(filter #(str/includes? % "repl"))
(map #(subs % 9 15))
(map str/trim))'


maybe also have a look at pgrep and pkill


A bit more verbose, but using pure Java:

;; destroy all nano processes of all users

(require '[clojure.string :as str])

(defn command [phandle]
  (.orElse (.command (.info phandle)) ""))

(map #(.destroyForcibly %)
     (filter #(str/includes? (command %) "nano")
             (iterator-seq (.iterator (java.lang.ProcessHandle/allProcesses)))))


if that last expression is maybe better with mapv or wrapped into a doall to avoid laziness or if we don't care about which processes has been killed, then just run!. there might also be some caveat with .destroyForcibly, if the process was not spawned by the current JVM process: > The default implementation of this method invokes destroy() and so may not forcibly terminate the process. Source:


@U086D6TBN I think they include that caveat since not all OSes may support all ops


sure, this was just a quick sketch, improve as you see fit


there is also jps -l -v, which already limits the process list to java processes and nicely prints their main class or with the -l both the main package and class (if i understand it correctly)


jps is part of JDKs or maybe even JREs?


I think so yes


✦2 $ jps -l | bb -i '(->> *input* (map #(clojure.string/split % #" ")) clojure.pprint/pprint)' 
(["1729" "org.jetbrains.idea.maven.server.RemoteMavenServer36"]
 ["3553" "jdk.jcmd/"]
 ["3241" "clojure.main"]


i see it often that ppl shell out from bb. that makes a bb program more self contained, but it's also a lot more verbose, because it's likely that u want to do what bb -i does anyway.


both use cases have pros and cons


sure, im just raising awareness of this nice -i option 🙂

👍 2

I'm working on getting to work from source. I've discovered a bug when you generate a case expression in a macro :-D


instead of list? I should have used seq? somewhere


i’m using the filewatcher pod to watch a couple of directories, but at the end of the script I need to do a (Thread/sleep somelargenumber) otherwise the script exits right away. Is there a cleaner way to pause indefinitely with out bb exiting?


@winkywooster @(promise) is usually what I do

👍 2

Hey borkdude, any chance to have java.text.SimpleDateFormat added to the Java classes available in bb? Sorry if this isn't the right place to ask this things


In bb the java.time package is available, this is recommended to use over java.text.SimpleDateFormat


I'm very close to making babashka compatible with executing from source (not sure if this is really useful since bb already has a JSON lib, but why not). There is one last issue with clojure.pprint and formatter-out which is used in for pprint-ing JSON. In case anyone wants to help debug... The fork of data.json here: runs with babashka's formatter-out branch. I'm calling it a night.


hello, whats the syntax to ask babashka to run a function given a namespace/fn-name?


similar to clojure -X, I remember there was something like that, but can't remember what the command was like


You mean using tasks?

  clean {:doc "Removes target folder"
         :requires ([babashka.fs :as fs])
         :task (fs/delete-tree "target")}
Then you can do a bb run clean


I remember at the beginning of tasks (and maybe the feature got removed, not sure) there was some option to run any function from any namespace in the classpath (kinda like -X on clojure), trying to remember about that


oh, I guess there are two sections on "main function" on the docs, and the link got to the later, but the first seems to be what I'm looking for, using -m


yes, -m foo/bar