mcuadros / ofelia

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

the less-than sign in command is escaped #180

Open zishiguo opened 2 years ago

zishiguo commented 2 years ago

My english is poor,docker-compose.yml, as follows:

version: "3"
services:
  ofelia:
    image: mcuadros/ofelia:latest
    depends_on:
      - rpa_db
    command: daemon --docker
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./ofelia/logs:/tmp/logs:rw
      - ./ofelia/config.ini:/etc/ofelia/config.ini:ro
    labels:
      ofelia.save-folder: "/tmp/logs"
      ofelia.job-local.save-only-on-error: false
    networks:
      - rpa

  rpa_db:
    image: mysql:5.7
    container_name: rpa_db
    restart: always
    labels:
      ofelia.enabled: "true"
      ofelia.job-exec.jobs-clean.schedule: "@every 1m"
      ofelia.job-exec.jobs-clean.command: "mysql -u root -prpa rpa_db < /root/clean.sql"
    environment:
      MYSQL_ROOT_PASSWORD: rpa
      MYSQL_USER: rpa
      MYSQL_PASSWORD: rpa
      LANG: "C.UTF-8" # 中文支持
    volumes:
      - ./db/data:/var/lib/mysql
      - ./db/config:/etc/mysql
      - ./db/db_init.sh:/usr/local/bin/db_init.sh
      - ./db/clean.sql:/root/clean.sql
    ports:
      - 3306:3306
    networks:
      - rpa

networks:
  rpa:
    driver: bridge

clean.sql as follows:

delete from jobs where startTime > 0 and unix_timestamp()-startTime > 30 * 24 * 3600 limit 1;

20220324_013352_jobs-clean.json as follows:

{
  "Execution": {
    "ID": "6301431e3736",
    "Date": "2022-03-24T01:33:52.0074305Z",
    "Duration": 135686700,
    "IsRunning": false,
    "Failed": true,
    "Skipped": false,
    "Error": {}
  },
  "Job": {
    "Schedule": "@every 1m",
    "Name": "jobs-clean",
    "Command": "mysql -u root -prpa rpa_db \u003c /root/clean.sql",
    "Container": "rpa_db",
    "User": "root",
    "TTY": false,
    "Environment": null,
    "NoOverlap": false,
    "SlackWebhook": "",
    "SlackOnlyOnError": false,
    "SaveFolder": "",
    "SaveOnlyOnError": false,
    "SMTPHost": "",
    "SMTPPort": 0,
    "SMTPUser": "",
    "SMTPPassword": "",
    "EmailTo": "",
    "EmailFrom": "",
    "MailOnlyOnError": false
  }
}

20220324_013352_jobs-clean.stderr.log as follows:

mysql: [Warning] Using a password on the command line interface can be insecure.

20220324_013352_jobs-clean.stdout.log as follows:

mysql  Ver 14.14 Distrib 5.7.36, for Linux (x86_64) using  EditLine wrapper
Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --auto-rehash       Enable automatic rehashing. One doesn't need to use
                      'rehash' to get table and field completion, but startup
                      and reconnecting may take a longer time. Disable with
                      --disable-auto-rehash.
                      (Defaults to on; use --skip-auto-rehash to disable.)
...

I execute mysql -u root -prpa rpa_db \u003c /root/clean.sql as literal in rpa_db container, got the same stdout.

I guess that the less-than sign is escaped for the reason. I hope you should understand my description.

zishiguo commented 2 years ago

I found a solution. I use shell script instead of command inside the docker-compose.yml

version: "3"
services:
    ...
  rpa_db:
    image: mysql:5.7
    container_name: rpa_db
    restart: always
    labels:
        ...
      ofelia.job-exec.jobs-clean.command: "./root/jobs_clean.sh"
    volumes:
      - ./db/jobs_clean.sh:/root/jobs_clean.sh
networks:
  rpa:
    driver: bridge

jobs_clean.sh as follows:

#!/bin/sh

echo 'delete from jobs where startTime > 0 and unix_timestamp()-startTime > 30 * 24 * 3600 limit 1;' | mysql -u root -prpa rpa_db