mcuadros / ofelia

A docker job scheduler (aka. crontab for docker)
MIT License
3.12k stars 179 forks source link

can't escape semicolon in config.ini #267

Closed edwardsun closed 7 months ago

edwardsun commented 7 months ago

Hi,

I'm trying to get the following example to work.

test.ini [job-run "job-executed-on-new-container"] schedule = @every 5s image = ubuntu:latest command = bash -c 'for name in {abc,def}; do echo \$name; done'

and from the console I ran: docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock:ro -v $(pwd)/test.ini:/config.ini:ro mcuadros/ofelia:latest daemon daemon --config=/config.ini

However, it seems the scheduler cannot capture the whole command:

2024-04-09T13:18:48.79Z scheduler.go:44 ▶ NOTICE New job registered "job-executed-on-new-container" - "bash -c 'for name in {abc,def}" - "@every 5s" 2024-04-09T13:18:48.802Z scheduler.go:55 ▶ DEBUG Starting scheduler with 1 jobs 2024-04-09T13:18:53.007Z common.go:125 ▶ NOTICE [Job "job-executed-on-new-container" (c49531375d43)] Started - bash -c 'for name in {abc,def} 2024-04-09T13:18:53.759Z common.go:125 ▶ NOTICE [Job "job-executed-on-new-container" (c49531375d43)] Pulled image ubuntu:latest 2024-04-09T13:18:55.776Z common.go:121 ▶ ERROR [Job "job-executed-on-new-container" (c49531375d43)] StdErr: bash: -c: line 2: syntax error: unexpected end of file 2024-04-09T13:18:55.778Z common.go:121 ▶ ERROR [Job "job-executed-on-new-container" (c49531375d43)] Finished in "2.769144295s", failed: true, skipped: false, error: error non-zero exit code: 2

Any suggestions?

taraspos commented 7 months ago

This seems to be a limitation of https://github.com/go-gcfg/gcfg/tree/v1. I would suggest to try docker labels approach instead, see this example:

version: "3"
services:
  ofelia:
    image: mcuadros/ofelia:latest
    depends_on:
      - ubuntu
    command: daemon --docker
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro

  ubuntu:
    image: ubuntu
    command: sleep 10000
    labels:
      ofelia.enabled: "true"
      ofelia.job-exec.datecron.schedule: "@every 5s"
      ofelia.job-exec.datecron.command: "bash -c 'for name in {abc,def}; do echo $$name; done'"
$ docker compose up

Attaching to ofelia-1, ubuntu-1
ofelia-1  | 2024-04-20T19:31:42.385Z  scheduler.go:34 ▶ NOTICE New job registered "datecron" - "bash -c 'for name in {abc,def}; do echo $name; done'" - "@every 5s"
ofelia-1  | 2024-04-20T19:31:42.385Z  scheduler.go:54 ▶ DEBUG Starting scheduler with 1 jobs
ofelia-1  | 2024-04-20T19:31:47.004Z  common.go:125 ▶ NOTICE [Job "datecron" (7eae2dec4c12)] Started - bash -c 'for name in {abc,def}; do echo $name; done'
ofelia-1  | 2024-04-20T19:31:47.074Z  common.go:125 ▶ NOTICE [Job "datecron" (7eae2dec4c12)] StdOut: abc
ofelia-1  | def
ofelia-1  | 2024-04-20T19:31:47.074Z  common.go:125 ▶ NOTICE [Job "datecron" (7eae2dec4c12)] Finished in "70.018662ms", failed: false, skipped: false, error: none