如何增加一个自己的 Linux demon
环境
Ubuntu Server 18.04 LTS (HVM)
Amazon 系统映像 (AMI)
ubuntu@ip-172-31-2-159:~$ hostnamectl
Static hostname: ip-172-31-2-159
Icon name: computer-vm
Chassis: vm
Machine ID: 0d5a12345
Boot ID: d69a12345
Virtualization: xen
Operating System: Ubuntu 18.04.1 LTS
Kernel: Linux 4.15.0-1019-aws
Architecture: x86-64
ubuntu@ip-172-31-2-159:~$ systemctl --version
systemd 237
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid
ubuntu@ip-172-31-2-159:~$ systemctl get-default
graphical.target
ubuntu@ip-172-31-2-159:~$ systemd-analyze critical-chain
The time after the unit is active or started is printed after the "@" character.
The time the unit takes to start is printed after the "+" character.
graphical.target @30.448s
└─multi-user.target @30.445s
└─snapd.seeded.service @6.428s +24.009s
└─snapd.service @22.570s +852ms
└─snap-amazon\x2dssm\x2dagent-295.mount @29.959s +20ms
└─local-fs-pre.target @853ms
└─keyboard-setup.service @320ms +510ms
└─systemd-journald.socket @247ms
└─system.slice @206ms
└─-.slice @204ms
ubuntu@ip-172-31-2-159:~$ systemd-analyze critical-chain sshd.service
The time after the unit is active or started is printed after the "@" character.
The time the unit takes to start is printed after the "+" character.
ssh.service +26ms
└─basic.target @5.955s
└─sockets.target @5.953s
└─snapd.socket @5.876s +70ms
└─sysinit.target @5.828s
└─cloud-init.service @4.772s +1.051s
└─systemd-networkd-wait-online.service @3.201s +1.565s
└─systemd-networkd.service @3.168s +28ms
└─network-pre.target @3.164s
└─cloud-init-local.service @474ms +2.686s
└─systemd-remount-fs.service @328ms +104ms
└─systemd-journald.socket @247ms
└─system.slice @206ms
└─-.slice @204ms
下面开始正文
在ssh.service后面添加一个自定义service 取名my-snell.service,作用是守护自定义的bin文件snell-server
下载snell 解压得到 snell-server
wget https://github.com/surge-networks/snell/releases/download/v1.1.0/snell-server-v1.1.0-linux-amd64.zip
编写my-snell.service
ubuntu@ip-172-31-2-159:~/snell$ vim my-snell.service
[Unit]
Description=An encrypted proxy service program
After=sshd.service
[Service]
ExecStart=-/home/ubuntu/snell/snell-server -c /home/ubuntu/snell/snell-server.conf
KillMode=process
Restart=on-failure
Type=simple
[Install]
WantedBy=multi-user.target
Alias=my-snell.service
放入/etc/systemd/system目录,使能,启动
ubuntu@ip-172-31-2-159:/etc/systemd/system$ sudo ln -s /home/ubuntu/snell/my-snell.service my-snell.service
ubuntu@ip-172-31-2-159:/etc/systemd/system$ sudo systemctl enable my-snell.service
Created symlink /etc/systemd/system/multi-user.target.wants/my-snell.service → /home/ubuntu/snell/my-snell.service.
ubuntu@ip-172-31-2-159:/etc/systemd/system$ sudo systemctl start my-snell.service
查看启动状态
ubuntu@ip-172-31-2-159:~$ sudo systemctl status my-snell.service
● my-snell.service - An encrypted proxy service program
Loaded: loaded (/home/ubuntu/snell/my-snell.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-03-19 14:06:21 UTC; 1min 44s ago
Main PID: 13102 (snell-server)
Tasks: 1 (limit: 1152)
CGroup: /system.slice/my-snell.service
└─13102 /home/ubuntu/snell/snell-server -c /home/ubuntu/snell/snell-server.conf
Mar 19 14:06:21 ip-172-31-2-159 systemd[1]: Started An encrypted proxy service program.
Mar 19 14:06:21 ip-172-31-2-159 snell-server[13102]: 2019-03-19 14:06:21.090170 [server_main] <NOTIFY> snell-server v1.1.0 (Mar 5 2019 13:50:06)
Mar 19 14:06:21 ip-172-31-2-159 snell-server[13102]: 2019-03-19 14:06:21.090257 [server_main] <NOTIFY> Start snell server on 0.0.0.0:40546
Mar 19 14:06:21 ip-172-31-2-159 snell-server[13102]: 2019-03-19 14:06:21.090266 [server_main] <NOTIFY> Obfs enabled (HTTP)
Mar 19 14:06:21 ip-172-31-2-159 snell-server[13102]: 2019-03-19 14:06:21.090333 [server_main] <NOTIFY> TCP Fast Open enabled
reboot&verify
➜ ssh nx1
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-1019-aws x86_64)
ubuntu@ip-172-31-2-159:~$ sudo systemctl status my-snell.service
● my-snell.service - An encrypted proxy service program
Loaded: loaded (/home/ubuntu/snell/my-snell.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-03-19 14:11:54 UTC; 12s ago
Main PID: 781 (snell-server)
Tasks: 1 (limit: 1152)
CGroup: /system.slice/my-snell.service
└─781 /home/ubuntu/snell/snell-server -c /home/ubuntu/snell/snell-server.conf
Mar 19 14:11:54 ip-172-31-2-159 snell-server[781]: 2019-03-19 14:11:54.329378 [server_main] <NOTIFY> snell-server v1.1.0 (Mar 5 2019 13:50:06)
Mar 19 14:11:54 ip-172-31-2-159 snell-server[781]: 2019-03-19 14:11:54.329916 [server_main] <NOTIFY> Start snell server on 0.0.0.0:40546
Mar 19 14:11:54 ip-172-31-2-159 snell-server[781]: 2019-03-19 14:11:54.329926 [server_main] <NOTIFY> Obfs enabled (HTTP)
Mar 19 14:11:54 ip-172-31-2-159 snell-server[781]: 2019-03-19 14:11:54.330081 [server_main] <NOTIFY> TCP Fast Open enabled
Mar 19 14:11:54 ip-172-31-2-159 systemd[1]: Started An encrypted proxy service program.
经验证重启后snell-server可以自行启动
查看log
journalctl --system | grep snell-server
参考文献
[search] Systemd site:ruanyifeng.com
http://www.ruanyifeng.com/blog/2016/02/lin...
http://www.ruanyifeng.com/blog/2016/03/sys...
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: