简体   繁体   English

计算机B中的服务启动并运行后,如何编写linux服务在计算机A上启动服务?(计算机A和B在同一网络中)

[英]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.

相关问题 仅当服务 A 存在时,Systemd 服务 B 才在另一个服务 A 之后启动 - Systemd service B to start after another service A only if Service A exists 在网络上的另一台计算机上启动进程 - Start a process on another computer on the network USBDevice没有启动Linux计算机 - USBDevice is not picking up linux computer 如何使用.Net Core从远程计算机,同一网络,linux获取mac-address - How to get mac-address from a remote computer, same network, at linux, using .Net Core 如何在linux程序和运行Wine(同一台计算机)的Windows程序之间共享内存? - How to share memory between linux program and windows program running through Wine (same computer)? 如何使用scp将文件从远程计算机(运行Linux)复制到Windows计算机? - How do I copy files from a remote computer (running Linux) to my Windows computer using scp? 如何从基于同一台计算机的 Linux VM 在 Windows 计算机上运行 Internet Explorer? - How do I run Internet Explorer on a Windows computer from a Linux VM based on the same computer? linux deploy,kali,在启动后启动应用程序服务 - linux deploy, kali, start an app service after its boots up 如何从运行在 Linux 上的 .NET 核心应用程序关闭计算机 - How to shutdown computer from a .NET Core application running on Linux 在Windows计算机上从Python运行Linux命令 - Running Linux Commands from Python on Windows Computer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM