# Need to define environment variable to calculate the previous date

**URL:** <https://discuss.elastic.co/t/need-to-define-environment-variable-to-calculate-the-previous-date/171233>\
**Category:** Logstash\
**Created:** [March 7, 2019, 6:18am UTC](https://discuss.elastic.co/t/need-to-define-environment-variable-to-calculate-the-previous-date/171233 "2019-03-07T06:18:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsingh](https://avatars.discourse-cdn.com/v4/letter/v/b4bc9f/32.png) [@vsingh](https://discuss.elastic.co/u/vsingh)\
**Post date:** [March 7, 2019, 6:18am UTC](https://discuss.elastic.co/t/need-to-define-environment-variable-to-calculate-the-previous-date/171233/1 "2019-03-07T06:18:03Z")

</div>

Hello there,

I need to import the prior date data to create the index in elastic from mysql.  
this is script:

input {  
jdbc {  
jdbc\_connection\_string =\> "jdbc:mysql://localhost:3306/testdb?zeroDateTimeBehavior=convertToNull"  
jdbc\_user =\> "root"  
jdbc\_password =\> "mysql"  
jdbc\_driver\_library =\> "C:\Users\mysql-connector-java-8.0.13.jar"  
jdbc\_driver\_class =\> "com.mysql.cj.jdbc.Driver"  
Pre\_DATE=`date --date="-1 day" '+%Y.%m.%d'`  
statement =\> "select transaction\_id from testdb.sample where process\_date==Pre\_DATE "  
type =\> "response"  
}  
}  
output {  
if [type] == "response" {  
elasticsearch {  
index =\> "index\_one"  
document\_type =\> "type"  
document\_id =\> "%{transaction\_id}"  
hosts =\> "localhost:9200"   
}  
stdout {  
codec =\> rubydebug  
}  
}  
}

this is log:

[2019-03-07T11:30:52,708][INFO][logstash.runner] Starting Logstash {"  
logstash.version"=\>"6.4.0"}  
[2019-03-07T11:30:53,223][ERROR][logstash.agent] Failed to execute ac  
tion {:action=\>LogStash::PipelineAction::Create/pipeline\_id:main, :exception=\>"L  
ogStash::ConfigurationError", :message=\>"Expected one of #, =\> at line 8, column  
11 (byte 352) after input {\n jdbc {\n jdbc\_connection\_string =\> "jd  
bc:mysql://localhost:3306/testdb?zeroDateTimeBehavior=convertToNull"\n j  
dbc\_user =\> "root"\n jdbc\_password =\> "mysql"\n jdbc\_driver\_li  
brary =\> "C:\Users\mysql-connector-java-8.0.13.j  
ar"\n jdbc\_driver\_class =\> "com.mysql.cj.jdbc.Driver"\n\t\tPre\_DATE",  
:backtrace=\>["C:/ELK/logstash/logstash-core/lib/logstash/compiler.rb:41:in `comp ile_imperative'", "C:/ELK/logstash/logstash-core/lib/logstash/compiler.rb:49:in`compile\_graph'", "C:/ELK/logstash/logstash-core/lib/logstash/compiler.rb:11:in  
`block in compile_sources'", "org/jruby/RubyArray.java:2486:in`map'", "C:/ELK/l  
ogstash/logstash-core/lib/logstash/compiler.rb:10:in `compile_sources'", "org/lo gstash/execution/AbstractPipelineExt.java:157:in`initialize'", "C:/ELK/logstash  
/logstash-core/lib/logstash/pipeline.rb:22:in `initialize'", "C:/ELK/logstash/lo gstash-core/lib/logstash/pipeline.rb:90:in`initialize'", "C:/ELK/logstash/logst  
ash-core/lib/logstash/pipeline\_action/create.rb:38:in `execute'", "C:/ELK/logsta sh/logstash-core/lib/logstash/agent.rb:309:in`block in converge\_state'"]}  
[2019-03-07T11:30:53,536][INFO][logstash.agent] Successfully started  
Logstash API endpoint {:port=\>9600}

please suggest where to define the environment variable.

Thanks.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 7, 2019, 3:30pm UTC](https://discuss.elastic.co/t/need-to-define-environment-variable-to-calculate-the-previous-date/171233/2 "2019-03-07T15:30:07Z")

</div>

> [@vsingh](#):
>
> ```
> Pre_DATE= `date --date="-1 day" '+%Y.%m.%d'`
> 
> ```

You would have to run that in the shell that invokes logstash, then refer to it using ${Pre\_DATE} in the statement option.

---

<div class="post-metadata">

**Author:** ![vsingh](https://avatars.discourse-cdn.com/v4/letter/v/b4bc9f/32.png) [@vsingh](https://discuss.elastic.co/u/vsingh)\
**Post date:** [March 8, 2019, 7:18am UTC](https://discuss.elastic.co/t/need-to-define-environment-variable-to-calculate-the-previous-date/171233/3 "2019-03-08T07:18:31Z")

</div>

Thanks for reply.

It is working if i call it through shell and refer it in logstash.  
i want to call logstash using cron to automate the process then where Pre\_DATE will be define.

kindly suggest.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 8, 2019, 1:01pm UTC](https://discuss.elastic.co/t/need-to-define-environment-variable-to-calculate-the-previous-date/171233/4 "2019-03-08T13:01:27Z")

</div>

Have cron call a shell script that sets it and then calls logstash.

---

<div class="post-metadata">

**Author:** ![vsingh](https://avatars.discourse-cdn.com/v4/letter/v/b4bc9f/32.png) [@vsingh](https://discuss.elastic.co/u/vsingh)\
**Post date:** [March 11, 2019, 5:41am UTC](https://discuss.elastic.co/t/need-to-define-environment-variable-to-calculate-the-previous-date/171233/5 "2019-03-11T05:41:00Z")

</div>

Can i define Pre\_DATE environment variable instead of the shell so that i can refer it in logstash configuration file.

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 8, 2019, 5:49am UTC](https://discuss.elastic.co/t/need-to-define-environment-variable-to-calculate-the-previous-date/171233/6 "2019-04-08T05:49:25Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
