Skip to content

Commit

Permalink
Use -interpreter to switch to /bin/sh for example (#5)
Browse files Browse the repository at this point in the history
`-interpreter` flag
  • Loading branch information
bobiverse authored Nov 10, 2023
1 parent 539eb28 commit 5222282
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
67 changes: 39 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,48 +58,59 @@ Output:
...
```

## Stop all jobs if error message occurs
Use `-stop-on` flag to halt all tasks upon encountering an error message.
Note: All tasks will be discontinued if any job yields a message indicating a stop condition.

```sh
./threadme -cmd 'bash sendmail.sh {{LINE}}' -f customers.txt -stop-on 'Error:'
```
Output:
```
2023/11/10 15:52:36 [0/8256] [bash sendmail.sh a@a.example.com] ==> [Sent: a@a.example.com]
2023/11/10 15:39:24 ERROR: [4/8256] [bash sendmail.sh e@e.example.com] ==> [Invalid user; exit status 1;]
2023/11/10 15:52:36 [2/8256] [bash sendmail.sh c@c.example.com] ==> [Sent: c@c.example.com]
2023/11/10 15:52:36 [1/8256] [bash sendmail.sh b@b.example.com] ==> [Sent: b@b.example.com]
2023/11/10 15:52:36 [3/8256] [bash sendmail.sh d@d.example.com] ==> [Sent: d@d.example.com]
2023/11/10 15:52:37 [4/8256] [bash sendmail.sh e@e.example.com] ==> [Error: timeout while sending e@e.example.com]
2023/11/10 15:03:57 > Stop output message found: Error: timeout while sending e@e.example.com
2023/11/10 15:52:37 > Stopping all workers!
2023/11/10 15:52:37 > Duration: 12.03473712s
exit status 1
```

## Keep running while message present
Use `-while=` flag to cease all tasks when a success message is absent from any of the jobs.
Use `-while` flag to cease all tasks when a success message is absent from any of the jobs.
_Note_: All jobs will be terminated if the message differs for even a single job.

```sh
./threadme -cmd 'bash sendmail.sh {{LINE}}' -f customers.txt -while 'Sent:'
```
Output:
```
2022/04/15 15:52:36 [0/8256] [bash sendmail.sh a@a.example.com] ==> [Sent: a@a.example.com]
2022/04/15 15:39:24 ERROR: [4/8256] [bash sendmail.sh e@e.example.com] ==> [Invalid user; exit status 1;]
2022/04/15 15:52:36 [2/8256] [bash sendmail.sh c@c.example.com] ==> [Sent: c@c.example.com]
2022/04/15 15:52:36 [1/8256] [bash sendmail.sh b@b.example.com] ==> [Sent: b@b.example.com]
2022/04/15 15:52:36 [3/8256] [bash sendmail.sh d@d.example.com] ==> [Sent: d@d.example.com]
2022/04/15 15:52:37 [4/8256] [bash sendmail.sh e@e.example.com] ==> [Error: timeout while sending e@e.example.com]
2022/04/15 15:52:37 > No `while message` found: Error: timeout while sending e@e.example.com
2022/04/15 15:52:37 > Stopping all workers!
2022/04/15 15:52:37 > Duration: 12.03404163s
2023/11/10 15:52:36 [0/8256] [bash sendmail.sh a@a.example.com] ==> [Sent: a@a.example.com]
2023/11/10 15:39:24 ERROR: [4/8256] [bash sendmail.sh e@e.example.com] ==> [Invalid user; exit status 1;]
2023/11/10 15:52:37 > No `while message` found: Invalid user; exit status 1;
2023/11/10 15:52:37 > Stopping all workers!
2023/11/10 15:52:37 > Duration: 7.03404163s
exit status 1
...
```


## Stop all jobs if error message occurs
Use `-stop-on=` flag to halt all tasks upon encountering an error message.
Note: All tasks will be discontinued if any job yields a message indicating a stop condition.
## Use different interpreter
Use `-interpreter` flag to change command interpreter. By default it's `/bin/bash`.
_Default_: `/bin/bash`

#### Have `$RANDOM` value
```sh
./threadme -cmd 'bash sendmail.sh {{LINE}}' -f customers.txt -stop-on 'Error:'
```
Output:
# default is `/bin/bash`
./threadme -cmd 'echo "$USER $RANDOM"' -n=1
# 2023/11/10 18:47:57 [0/1] [echo $USER $RANDOM] ==> [bobiverse 31459]
```
2022/04/15 15:52:36 [0/8256] [bash sendmail.sh a@a.example.com] ==> [Sent: a@a.example.com]
2022/04/15 15:39:24 ERROR: [4/8256] [bash sendmail.sh e@e.example.com] ==> [Invalid user; exit status 1;]
2022/04/15 15:52:36 [2/8256] [bash sendmail.sh c@c.example.com] ==> [Sent: c@c.example.com]
2022/04/15 15:52:36 [1/8256] [bash sendmail.sh b@b.example.com] ==> [Sent: b@b.example.com]
2022/04/15 15:52:36 [3/8256] [bash sendmail.sh d@d.example.com] ==> [Sent: d@d.example.com]
2022/04/15 15:52:37 [4/8256] [bash sendmail.sh e@e.example.com] ==> [Error: timeout while sending e@e.example.com]
2023/11/10 15:03:57 > Stop output message found: Error: timeout while sending e@e.example.com
2022/04/15 15:52:37 > Stopping all workers!
2022/04/15 15:52:37 > Duration: 12.03473712s
exit status 1
...

#### Don't have `$RANDOM` value
```sh
./threadme -cmd 'echo "$USER $RANDOM"' -n=1 -interpreter=/bin/sh
# 2023/11/10 18:47:57 [0/1] [echo $USER $RANDOM] ==> [bobiverse]
```

8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module github.com/briiC/threadme

go 1.18
go 1.21

require golang.org/x/net v0.18.0

require (
github.com/fatih/semgroup v1.2.0 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
github.com/fatih/semgroup v1.2.0
golang.org/x/net v0.18.0
)

require golang.org/x/sync v0.5.0 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ github.com/fatih/semgroup v1.2.0 h1:h/OLXwEM+3NNyAdZEpMiH1OzfplU09i2qXPVThGZvyg=
github.com/fatih/semgroup v1.2.0/go.mod h1:1KAD4iIYfXjE4U13B48VM4z9QUwV5Tt8O4rS879kgm8=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"time"
)

var cmdInterpreter = "/bin/bash"

const example = "`./threadme -t5 -cmd 'echo \"{{N}}:{{LINE}}\"'`"

func main() {
Expand All @@ -27,8 +29,13 @@ func main() {
n := flag.Int("n", 100, "If no file given this is how many jobs will be performed")
stopOnMsg := flag.String("stop-on", "", "If any job gets this message, all stops")
whileMsg := flag.String("while", "", "Keep running while all have this message")
interpreter := flag.String("interpreter", cmdInterpreter, "Interpreter for command")
flag.Parse()

if *interpreter != "" {
cmdInterpreter = *interpreter
}

*cmd = strings.TrimSpace(*cmd)
if *cmd == "" {
log.Fatalf("Empty command. %s", example)
Expand Down Expand Up @@ -188,10 +195,10 @@ func runBashWithTimeout(timeout time.Duration, cmdstr string) ([]byte, []byte, e
// Run command in env as whole
// Useful when need to execute command with wildcard so these characters
// is not treated as string
name := "/bin/sh"
name := cmdInterpreter
args := []string{
"-c",
strings.TrimPrefix(cmdstr, "/bin/sh -c "), // as one argument
strings.TrimPrefix(cmdstr, cmdInterpreter+" -c "), // as one argument
}

cmd := exec.Command(name, args...) // #nosec G204
Expand Down

0 comments on commit 5222282

Please sign in to comment.