Fork me on GitHub
#babashka
<
2021-07-07
>
bherrmann01:07:58

What is the most "babashka" way to do,

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

bherrmann01:07:08

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"))
str/split-lines
(filter #(str/includes? % "repl"))
(map #(subs % 9 15))
(map str/trim))'

holger06:07:41

maybe also have a look at pgrep and pkill

borkdude07:07:37

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)))))

onetom14:07:38

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: https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html#destroyForcibly--

borkdude14:07:43

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

borkdude14:07:10

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

onetom14:07:57

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)

onetom14:07:22

jps is part of JDKs or maybe even JREs?

borkdude14:07:15

I think so yes

onetom14:07:15

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

onetom15:07:30

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.

borkdude15:07:22

both use cases have pros and cons

onetom15:07:06

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

👍 2
borkdude16:07:42

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

borkdude16:07:53

instead of list? I should have used seq? somewhere

winkywooster18:07:18

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?

borkdude18:07:24

@winkywooster @(promise) is usually what I do

👍 2
rafaeldelboni21:07:42

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

borkdude21:07:46

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

borkdude21:07:17

I'm very close to making babashka compatible with executing clojure.data.json 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 clojure.data.json for pprint-ing JSON. In case anyone wants to help debug... https://github.com/babashka/babashka/issues/922 The fork of data.json here: https://github.com/babashka/data.json runs with babashka's formatter-out branch. I'm calling it a night.

wilkerlucio23:07:36

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

wilkerlucio23:07:25

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

rafaeldelboni01:07:38

You mean using tasks?

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

wilkerlucio02:07:17

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

wilkerlucio02:07:17

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

borkdude07:07:20

yes, -m foo/bar