[英]how to write a linux service to start a service on computer A after a service in computer B is up and running?(computer A and B are in same network)
in my senario I have an linux service on computer that need to run after a service in computer B(computer A and B are in same network).how can I use system.d to do this job?在我的情况下,我在计算机上有一个 linux 服务,需要在计算机 B 中的服务之后运行(计算机 A 和 B 在同一网络中)。我如何使用 system.d 来完成这项工作?
There are several solutions to this scenario, the simplest solution is to write a bash script like bellow.这种情况有几种解决方案,最简单的解决方案是编写一个 bash 脚本,如下所示。 before that you should set
ssh-key
between 2 linux computer.在此之前,您应该在 2 台 linux 计算机之间设置
ssh-key
。
at first open a file.首先打开一个文件。
nano /usr/bin/script.sh
and Put the following line in it.并将以下行放入其中。
#!/bin/bash
ssh root@computerA 'systemctl start YOURServicName'
now you need to run this bash Script as SystemD Service in computer B.现在您需要在计算机 B 中将这个 bash 脚本作为 SystemD 服务运行。
[Unit]
Description=My Shell Script
[Service]
ExecStart=/usr/bin/script.sh
[Install]
WantedBy=multi-user.target
for more info use this link.有关更多信息,请使用此链接。
Finally You need to add this line in your B.service file:最后,您需要在 B.service 文件中添加这一行:
After=A.service
I hope my explanation was useful.我希望我的解释是有用的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.