比较来自世界各地的卖家的域名和 IT 服务价格

使用时通过套接字向服务器发送消息 Upstart

问题

在运行计算机时,我需要通过TCP套接字向服务器发送消息。 我们用 Ubuntu 14.04 因此,应使用默认值 Upstart 作为系统初始化。 (我们还有其他计算机运行 Ubuntu 16.04, 可以使用它 systemd, 所以我尝试与系统初始化文件分开存储shell脚本)

当前的解决方案

目前,我对客户使用两个文件:popping文件 .conf 和shell脚本文件。

粉红色的文件

粉红色的文件 (让我们称之为 foo.conf) 它具有以下内容:


#!upstart
description "Send Message on Startup"

start on (local-filesystems
and net-device-up
and runlevel [2345])

exec /opt/foo/foo.sh

文件壳

文件壳 (让我们称之为 foo.sh) 它具有以下内容


#!/bin/bash

echo "Sending update message..."
echo "Message" | nc server-hostname 9999
echo "Completed sending update message."

症状

当我重新启动这些文件的计算机时,我在日志文件中收到以下内容:


Sending update message...
Completed sending update message.

但是,服务器永远不会收到一条消息。

问题

目前,此解决方案不起作用。 我正在寻找关于如何使这一解决方案工作的建议,或其他建议执行相同的任务。

更新:文件 systemd

以下是服务单元文件的详细信息 systemd, 我打开了 Ubuntu 16.04. 这适用于每次重新启动。


[Unit]
Description=Send Message on Startup
After=network-online.target

[Service]
Type=oneshot
ExecStart=/opt/foo/foo.sh

[Install]
WantedBy=multi-user.target
已邀请:

冰洋

赞同来自:

试试这个:

#!upstart
description "Send Message on Startup"

start on (local-filesystems
and net-device-up IFACE!=lo
and runlevel [2345])

以下是解决此问题的另一种选择。 基本上,等到他回应ping。

#!/bin/bash

server_hostname='server_hostname'
ping -c 2 $server_hostname
while [ $? -ne 0 ]
do
echo 'Waiting for server...'
sleep 2
ping -c 2 $server_hostname
done

echo "Sending update message..."
echo "Message" | nc server-hostname 9999
echo "Completed sending update message."

要回复问题请先登录注册