Search
Close this search box.

Elasticsearch command line tools – elasticsearch-croneval

Table of Contents

1. Introduction

In the article Sent Christmas Card with Elasticsearch Watcher you have learned how to create watch and schedule it using non-cron expressions.  In case you want to define time with cron expression it is good to check if what you defined is exactly what you meant.

There is dedicated command line tool that let you achieve a goal called elasticsearch-croneval

2. Using tool without starting Elasticsearch

2.1. Format

Format of expression is <seconds> <minutes> <hours> <day_of_month> <month> <day_of_week> with optional year at the end

for example below will execute only in 2038

				
					docker run --rm \
docker.elastic.co/elasticsearch/elasticsearch:8.11.1 elasticsearch-croneval "8 14 3 19 1 ? 2038" -c 10
				
			
				
					Valid!
Now is [Fri, 15 Dec 2023 01:33:21] in UTC, local time is [Fri, 15 Dec 2023 01:33:21 +0000]
Here are the next 10 times this cron expression will trigger:
1.	Tue, 19 Jan 2038 03:14:08
	Tue, 19 Jan 2038 03:14:08 +0000
				
			

2.2. Running

To run this tool you don’t need to start Elasticsearch even

				
					docker run --rm \
docker.elastic.co/elasticsearch/elasticsearch:8.11.1 elasticsearch-croneval "0 0 12 29 2 ?" -c 10
				
			

Above command schedule to run every 29th of February at 12:00 and will give a results

				
					Valid!
Now is [Thu, 14 Dec 2023 12:46:21] in UTC, local time is [Thu, 14 Dec 2023 12:46:21 +0000]
Here are the next 10 times this cron expression will trigger:
1.	Thu, 29 Feb 2024 12:00:00
	Thu, 29 Feb 2024 12:00:00 +0000
2.	Tue, 29 Feb 2028 12:00:00
	Tue, 29 Feb 2028 12:00:00 +0000
3.	Sun, 29 Feb 2032 12:00:00
	Sun, 29 Feb 2032 12:00:00 +0000
4.	Fri, 29 Feb 2036 12:00:00
	Fri, 29 Feb 2036 12:00:00 +0000
5.	Wed, 29 Feb 2040 12:00:00
	Wed, 29 Feb 2040 12:00:00 +0000
6.	Mon, 29 Feb 2044 12:00:00
	Mon, 29 Feb 2044 12:00:00 +0000
7.	Sat, 29 Feb 2048 12:00:00
	Sat, 29 Feb 2048 12:00:00 +0000
8.	Thu, 29 Feb 2052 12:00:00
	Thu, 29 Feb 2052 12:00:00 +0000
9.	Tue, 29 Feb 2056 12:00:00
	Tue, 29 Feb 2056 12:00:00 +0000
10.	Sun, 29 Feb 2060 12:00:00
	Sun, 29 Feb 2060 12:00:00 +0000
				
			

3. Using tool with existing container

But if you have already Elasticsearch instance up and running you can call it there.

3.1. Start Elasticsearch

I want to show you on watch example how cron expressions are working so start your instance with xpack license on.

				
					docker run --rm \
--name elk01 \
-d \
-e xpack.license.self_generated.type=trial \
-e node.name="elk01" \
-p 9200:9200 \
-e ES_JAVA_OPTS="-Xms2g -Xmx2g" \
docker.elastic.co/elasticsearch/elasticsearch:8.11.1
				
			

set password for elastic user

				
					docker exec -it elk01 bash -c "(mkfifo pipe1); ( (elasticsearch-reset-password -u elastic -i < pipe1) & ( echo $'y\n123456\n123456' > pipe1) );sleep 5;rm pipe1"
				
			

3.2. Validate cron expression

Connect to running container and execute

				
					docker exec -it elk01 bash -c "elasticsearch-croneval '0 27-29 20 * * ?' -c 3"
				
			

example response:

				
					Valid!
Now is [Thu, 14 Dec 2023 20:26:22] in UTC, local time is [Thu, 14 Dec 2023 20:26:22 +0000]
Here are the next 3 times this cron expression will trigger:
1.	Thu, 14 Dec 2023 20:27:00
	Thu, 14 Dec 2023 20:27:00 +0000
2.	Thu, 14 Dec 2023 20:28:00
	Thu, 14 Dec 2023 20:28:00 +0000
3.	Thu, 14 Dec 2023 20:29:00
	Thu, 14 Dec 2023 20:29:00 +0000
				
			

4. Scheduling watch action

Validated cron expression will work as expected now, just run below

				
					curl -k -u elastic:123456 -XPUT "https://localhost:9200/_watcher/watch/test" \
-H 'content-type: application/json' -d'
{
    "trigger": {
        "schedule": {
            "cron": "0 27-29 20 * * ?"
        }
    },
    "condition": {
        "always": {}
    },
    "actions": {
        "log": {
            "logging": {
                "text": "executing at {{ctx.trigger.scheduled_time}} "
            }
        }
    }
}'
				
			

Check logs of container to see entries

				
					docker logs elk01 | grep "executing at"
				
			

You will find below entries:

				
					{"@timestamp":"2023-12-14T20:27:00.185Z", "log.level": "INFO", "message":"executing at 2023-12-14T20:27Z ", "ecs.version": "1.2.0","service.name":"ES_ECS","event.dataset":"elasticsearch.server","process.thread.name":"elasticsearch[elk01][watcher][T#6]","log.logger":"org.elasticsearch.xpack.watcher.actions.logging.ExecutableLoggingAction","elasticsearch.cluster.uuid":"DFL2HLFQT5GD-Y4jafBXMg","elasticsearch.node.id":"7X8Q9UmLTeiKb76qMdxBZg","elasticsearch.node.name":"elk01","elasticsearch.cluster.name":"docker-cluster"}
{"@timestamp":"2023-12-14T20:28:00.112Z", "log.level": "INFO", "message":"executing at 2023-12-14T20:28Z ", "ecs.version": "1.2.0","service.name":"ES_ECS","event.dataset":"elasticsearch.server","process.thread.name":"elasticsearch[elk01][watcher][T#7]","log.logger":"org.elasticsearch.xpack.watcher.actions.logging.ExecutableLoggingAction","elasticsearch.cluster.uuid":"DFL2HLFQT5GD-Y4jafBXMg","elasticsearch.node.id":"7X8Q9UmLTeiKb76qMdxBZg","elasticsearch.node.name":"elk01","elasticsearch.cluster.name":"docker-cluster"}
{"@timestamp":"2023-12-14T20:29:00.067Z", "log.level": "INFO", "message":"executing at 2023-12-14T20:29Z ", "ecs.version": "1.2.0","service.name":"ES_ECS","event.dataset":"elasticsearch.server","process.thread.name":"elasticsearch[elk01][watcher][T#8]","log.logger":"org.elasticsearch.xpack.watcher.actions.logging.ExecutableLoggingAction","elasticsearch.cluster.uuid":"DFL2HLFQT5GD-Y4jafBXMg","elasticsearch.node.id":"7X8Q9UmLTeiKb76qMdxBZg","elasticsearch.node.name":"elk01","elasticsearch.cluster.name":"docker-cluster"}
				
			

Which works as expected.

5. Final thoughts

In this article you have explored ways of calling croneval tool. Evaluating cron expression will be useful for you as it let you know what are exactly future dates.

Follow me on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

Share the Post:

Enjoy Free Useful Amazing Content

Related Posts