A Sample Socket-Based Activation
来源:互联网 发布:vm虚拟机装mac 编辑:程序博客网 时间:2024/05/16 08:46
This is an example of a socket-activated per-connection service (which is usually referred to as inetd-like service).A thorough explanation can be found athttp://0pointer.de/blog/projects/inetd.html.
Define a socket unit
The key point here is to specify Accept=yes
, which will make the socket accept connections (behaving like inetd) and passonly the resulting connection socket to the service handler.
Create /etc/systemd/system/baz.socket
:
[Unit]Description=Baz Socket[Socket]ListenStream=127.0.0.1:9999Accept=yes[Install]WantedBy=sockets.target
Define a service template unit
We now create a service template from which the actual service will be instantiated on-demand. The lifetime of this service isusually very short (thus it may not appear atsystemctl
).
Create /etc/systemd/system/baz@.service
:
[Unit]Description=Baz ServiceRequires=baz.socket[Service]Type=simpleExecStart=/usr/bin/python /opt/baz-service/serve.py %iStandardInput=socketStandardError=journalTimeoutStopSec=5[Install]WantedBy=multi-user.target
An example service handler would be (located at /opt/baz-service/serve.py
as specified at the service unit file):
#!/usr/bin/pythonimport sysimport logginglogging.basicConfig(level=logging.INFO)instance = sys.argv[1]# The connected socket is duplicated to stdin/stdoutdata = sys.stdin.readline().strip()logging.info('baz-service: at instance %s, got request: %s', instance, data)sys.stdout.write(data.upper() + '\r\n')
Test
Start (or enable it to start at boot time) the socket (verify it via systemctl status baz.socket
), and make a request to the service:
echo Hello| nc localhost 9999
You 'll notice that the socket's status has changed (i.e. Accepted: 1
). Also, any log message generated by the on-demandservice should be present at the journal (e.g.journalctl --all| grep -e baz
).
systemctl status baz.socket● baz.socket - Baz Socket Loaded: loaded (/etc/systemd/system/baz.socket; enabled; vendor preset: enabled) Active: active (listening) since Mon 2017-04-10 00:08:22 CST; 1h 13min ago Listen: 127.0.0.1:9999 (Stream) Accepted: 3; Connected: 0Apr 10 00:08:22 china systemd[1]: Listening on Baz Socket.
----------------------
To enable socket, run command:
systemctl enable baz.socket
systemctl start baz.socket
https://gist.github.com/drmalex07/28de61c95b8ba7e5017c
- A Sample Socket-Based Activation
- socket sample
- 【论文笔记】Margin Sample Mining Loss: A Deep Learning Based Method for Person Re-identification
- 【Person Re-ID】Margin Sample Mining Loss: A Deep Learning Based Method for Person Re-identification
- Sample Clock Based On UWP
- How To Create A Socket Based iPhone App and Server
- Peer2Peer socket sample
- socket通信sample
- Socket Server Client Sample
- Sample socket communication program
- Socket TCP/IP Sample
- A XSLT Sample
- a simple jsm sample
- A sample of Generics.
- A sample of indexers.
- a sample of Interators
- A simple Hibernate sample
- A sample for map
- (int*)(&a+1)
- MacOS Sierra10.12.4编译Android7.1.1源码必须跳的坑
- 报表绘图插件echarts使用教程
- ajax data的参数
- 【unity实用技能】unity编辑器工具之加载预制(Prefab)和场景(Scene)
- A Sample Socket-Based Activation
- 展示一款创视微前年开发的互联网家庭k歌硬件MC-1
- 欢迎使用CSDN-markdown编辑器
- 密集芯片的焊接技巧:从LQFP64说起
- 多层布局禁止底层获取点击事件
- 多线程之线程同步
- NancyFx的框架使用(一)
- java 发送rest消息 tls 被服务器断开连接
- 迷途指针和空指针