लोड हो रहा है...... कृपया प्रतीक्षा करें........

July 21, 2023

How to delay the start of the systemd service

How to delay the start of the systemd service

1. check the status of service.

root@rsupernova:# systemctl status elasticsearch

● elasticsearch.service - Elasticsearch
     Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-07-21 07:15:08 UTC; 2min 23s ago
TriggeredBy: ● elasticsearch.timer
       Docs: https://www.elastic.co
   Main PID: 3020 (java)
      Tasks: 59 (limit: 9411)
     Memory: 4.5G
     CGroup: /system.slice/elasticsearch.service
             ├─3020 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.enco>
             └─3208 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

Jul 21 07:13:55 elasticsearch01-qa-mi4-aws systemd[1]: Starting Elasticsearch...
Jul 21 07:15:08 elasticsearch01-qa-mi4-aws systemd[1]: Started Elasticsearch.

You can create a .timer systemd unit file to control the execution of your .service unit file.

So for example, to wait for 1 minute after boot-up before starting your elasticsearch.service, create a elasticsearch.timer file in the same directory with the below contents.

Note: that the timer uses the same service name

root@rsupernova:# cd /lib/systemd/system
root@rsupernova:# nano elasticsearch.timer
[Unit]
Description=Timer for the elasticsearch service

[Timer]
OnBootSec=1min

[Install]
WantedBy=timers.target
EOF

Save the file and exit.

It is important that the service is disabled (so it doesn’t start at boot), and the timer enabled, for all this to work.

root@rsupernova:# systemctl disable elasticsearch.service

output:

Synchronizing state of elasticsearch.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable elasticsearch
Removed /etc/systemd/system/multi-user.target.wants/elasticsearch.service.

Enable timer

root@rsupernova:# systemctl enable elasticsearch.timer

output:

Created symlink /etc/systemd/system/timers.target.wants/elasticsearch.timer → /lib/systemd/system/elasticsearch.timer.

Reload the systemctl Daemon and start the timer.

root@rsupernova:# systemctl daemon-reload
root@rsupernova:# systemctl start elasticsearch.timer

Now you can restart the server and check the status of elasticsearch service.

root@rsupernova:# systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
     Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
TriggeredBy: ● elasticsearch.timer

The Elasticsearch service will start automatically after 1 minute of system uptime. Timer will trigger it automatically.

Check the current timers using below command.

root@rsupernova:# systemctl list-timers

Output:

Posted in TutorialTaggs:
Write a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.