Linux 添加 systemctl 服务

警告
本文最后更新于 2020-08-06,文中内容可能已过时。

方法

新建一个服务文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
cd /usr/lib/systemd/system

$ls -alh
EasyMonitor.service  xtp_md.service

total 16K
drwxr-xr-x  2 root root 4.0K Aug  6 13:37 ./
drwxr-xr-x 10 root root 4.0K Jun  8 16:25 ../
-rw-r--r--  1 root root  998 Jan  7  2020 EasyMonitor.service
-rwxr-xr-x  1 root root  292 Aug  6 13:54 xtp_md.service*

设置 xtp_md.sevice 服务内容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[Unit]
Description=XTP mkdata
After=network.target network-online.target

[Service]
Type=forking
Restart=always
RestartSec=5
ExecStart=/home/william/workspace/highfort/xtp_md_start.sh
ExecReload=/home/william/workspace/highfort/xtp_md_restart.sh
ExecStop=/home/william/workspace/highfort/xtp_md_stop.sh

[Install]
WantedBy=multi-user.target

编写相应的执行文件

xtp_md_start.sh

1
2
3
4
5
#!/bin/bash

export LD_LIBRARY_PATH=/home/william/workspace/highfort/hippo/external/exch/XTP:/usr/lib/x86_64-linux-gnu:/usr/local/lib:$LD_LIBRARY_PATH
cd /home/william/workspace/highfort/hippo/build/install/bin
nohup ./md /home/william/workspace/highfort/hippo/app/gw/xtp_gw/config/xtp_test_william.conf &

xtp_md_stop.sh

1
2
3
#!/bin/bash

ps aux | grep "xtp_test_william.conf" | grep -v color | awk '{print $2}' | xargs kill -9

xtp_md_restart.sh

1
2
3
4
5
#!/bin/bash

export LD_LIBRARY_PATH=/home/william/workspace/highfort/hippo/external/exch/XTP:/usr/lib/x86_64-linux-gnu:/usr/local/lib:$LD_LIBRARY_PATH
cd /home/william/workspace/highfort/hippo/build/install/bin
nohup ./md /home/william/workspace/highfort/hippo/app/gw/xtp_gw/config/xtp_test_william.conf &

添加执行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## 重新载入
sudo systemctl daemon-reload

sudo systemctl enable xtp_md
sudo systemctl start xtp_md
sudo systemctl status xtp_md
sudo systemctl stop xtp_md

## 显示所有服务
sudo systemctl --type=service

systemctl
systemctl

相关内容

william 支付宝支付宝
william 微信微信
0%