On-Demand Activation with System-D Sockets

2012-03-23T10:08:44+08:00 | 2分钟阅读 | 更新于 2012-03-23T10:08:44+08:00

Macro Zhao

On-Demand Activation with System-D Sockets

推荐超级课程:

In this article I will showcase a template to activate an arbitrary application once it is accessed and deactivate it again automatically if it’s no longer used. While this is not difficult per se, it unfortunately requires a number of systemd units and can be confusing at first. If you only want the code go to my GitHub .

Overview

Systemd Socket Activation Overview

Actual service

Assuming we have a file /usr/bin/application.sh that runs continuously once started and provides a service on a Unix or network socket, we first need a systemd unit that starts this program.

[Unit]
Description=ScriptWapper
Requires=check-deactivate.service

[Service]
Type=simple
ExecStart=/usr/bin/application.sh
ExecStop=pkill application

[Install]
WantedBy=multi-user.target
Systemd socket proxy

We now need a systemd managed socket which can proxy connections, meaning forward thus connections from the systemd socket to the actual applications once they are activated. Some programs don’t need this intermediary and support systemd sockets directly, but many don’t.

[Unit]
Description=Socket Proxy for script-wrapper
After=script-wrapper.service
Requires=script-wrapper.service

[Service]
ExecStart=/lib/systemd/systemd-socket-proxyd 127.0.0.1:ANYPORT

[Install]
WantedBy=multi-user.target
Systemd socket

Then we need the actual systemd socket that kicks off every other unit once a it receives a connection attempt.

[Unit]
Description=Socket activator for application.sh
PartOf=script-wrapper-proxy.service

[Socket]
ListenStream=ANYPORT
BindIPv6Only=both
Accept=false

[Install]
WantedBy=multi-user.target

Checking deactivation condition

A “type simple” systemd-service expects the program to run continuously until stopped. It would be nice if our script would just “know” by itself, if it can stop. However if you apply this solution to an existing application it might be preferable to have an external, second script which is regularly called to check if the service should continue. For this we need a systemd-timer and unfortunately, because a timer can’t call a script, but only another Unit, we also need a wrapper for the script just like for our application.sh.

In our case we call this unit check-deactivate.service and put as a requirement of our ScriptWrapper.service to start it, as soon asm the ScriptWrapper unit is started.

The script is expected to stop once it has finished checking the status and act accordingly (meaning stopped the service or let it run). It therefore should be of type oneshot.

# /usr/bin/checker.sh
if [ users -gt o]
    systemctl stop ScriptWrapper

-----------------------------------------

[Unit]
Description=check if unit can be stopped

[Service]
Type=oneshot
ExecStart=/usr/local/bin/checker.sh

And the timer with correct dependencies:

[Unit]
Description=call checker-service
After=script-wrapper.service
PartOf=script-wrapper.service

[Timer]
OnUnitActiveSec=15min
OnActiveSec=14min
Persistent=false
Unit=script-wrapper-checker.service

Other Considerations

Timeouts

Depending on how long the application needs to start, it might be that the client side application perceives the connection attempt as a timeout. This essentially means that the client has to reinitiate the connection, which might be perceived as bad user experience in some cases.

Costs of start and stop

Starting and stopping some applications may not fare well against just letting it idle. Consider carefully if the saved idle time is worth the activation and deactivation costs.

Racecondition between shutdown and startup request

If a request is made during the servers services shutdown, the service will not restart. This could be circumvented, by having a startup script capable of waiting for the shutdown. Though, even if this does happen, it only means a client has to do another connection attempt.

© 2011 - 2025 Macro Zhao的分享站

关于我

如遇到加载502错误,请尝试刷新😄

Hi,欢迎访问 Macro Zhao 的博客。Macro Zhao(或 Macro)是我在互联网上经常使用的名字。

我是一个热衷于技术探索和分享的IT工程师,在这里我会记录分享一些关于技术、工作和生活上的事情。

我的CSDN博客:
https://macro-zhao.blog.csdn.net/

欢迎你通过评论或者邮件与我交流。
Mail Me

推荐好玩(You'll Like)
  • AI 动·画
    • 这是一款有趣·免费的能让您画的画中的角色动起来的AI工具。
    • 支持几十种动作生成。
我的项目(My Projects)
  • 爱学习网

  • 小乙日语App

    • 这是一个帮助日语学习者学习日语的App。
      (当然初衷也是为了自用😄)
    • 界面干净,简洁,漂亮!
    • 其中包含 N1 + N2 的全部单词和语法。
    • 不需注册,更不需要订阅!完全免费!
  • 小乙日文阅读器

    • 词汇不够?照样能读日语名著!
    • 越读积累越多,积跬步致千里!
    • 哪里不会点哪里!妈妈再也不担心我读不了原版读物了!
赞助我(Sponsor Me)

如果你喜欢我的作品或者发现它们对你有所帮助,可以考虑给我买一杯咖啡 ☕️。这将激励我在未来创作和分享更多的项目和技术。🦾

👉 请我喝一杯咖啡

If you like my works or find them helpful, please consider buying me a cup of coffee ☕️. It inspires me to create and share more projects in the future. 🦾

👉 Buy me a coffee