systemd管理服务

[root@zbs-staging-api system]# cat /lib/systemd/system/ncmulti@.service 
[Unit]
Description=many on %i
After=network.target

[Service]
PIDFile=/var/run/nc.%i.pid
#Type=forking
ExecStart=/usr/bin/nc -l %i # 命令行执行时在@后传递变量

[Install]
WantedBy=multi-user.target


#一条命令启动一个进程

[root@zbs-staging-api system]# systemctl start ncmulti@5733
[root@zbs-staging-api system]# systemctl start ncmulti@5734
[root@zbs-staging-api system]# systemctl start ncmulti@5732

[root@zbs-staging-api system]# ps aux | grep nc
root 19599 0.0 0.0 45676 2080 ? Ss 20:24 0:00 /usr/bin/nc -l 5735
root 19616 0.0 0.0 45676 2080 ? Ss 20:24 0:00 /usr/bin/nc -l 5733
root 19622 0.0 0.0 45676 2076 ? Ss 20:24 0:00 /usr/bin/nc -l 5734
root 19628 0.0 0.0 45676 2080 ? Ss 20:24 0:00 /usr/bin/nc -l 5732

#一条命令启动多个进程

[root@zbs-staging-api system]# systemctl start ncmulti@{9283,9282,9284} 或者 systemctl start ncmulti@{9282..9284}

[root@zbs-staging-api system]# ps aux | grep nc
root 19948 0.0 0.0 45676 2076 ? Ss 20:26 0:00 /usr/bin/nc -l 9283
root 19949 0.0 0.0 45676 2076 ? Ss 20:26 0:00 /usr/bin/nc -l 9282
root 19950 0.0 0.0 45676 2076 ? Ss 20:26 0:00 /usr/bin/nc -l 9284

[root@zbs-staging-api system]# systemctl restart ncmulti@{9283,9282,9284} #批量重启
[root@zbs-staging-api system]# ps aux | grep nc
root 19959 0.0 0.0 45676 2080 ? Ss 20:27 0:00 /usr/bin/nc -l 9283
root 19962 0.0 0.0 45676 2076 ? Ss 20:27 0:00 /usr/bin/nc -l 9282
root 19963 0.0 0.0 45676 2076 ? Ss 20:27 0:00 /usr/bin/nc -l 9284

# 启动多进程传递有序参数

systemctl start storage@sd{b..m}

原文地址:https://www.cnblogs.com/yooma/p/8244776.html