1

Problem

I need to send a message to a a server via a TCP socket during computer startup. We are using Ubuntu 14.04 and therefore, by default, must use Upstart as the system initialization. (We also have other computers running Ubuntu 16.04 that can use systemd, so I'm trying to keep the shell scripts separate from the system initialization file)

Current Solution

Currently I'm using two files for the clients: an upstart .conf file and a shell script file.

Upstart file

The upstart file (we will call it foo.conf) has the following contents:


#!upstart
description "Send Message on Startup"

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

exec /opt/foo/foo.sh

Shell File

The shell file (we will call it foo.sh) has the following contents


#!/bin/bash

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

Symptoms

When I reboot the computer that has these files, I get the following in the log file:


Sending update message...
Completed sending update message.

However, the server never receives the message.

The Question

Currently, this solution is not working. I'm looking for suggestions on either how to get this solution to work or other suggestions to accomplish the same task.

UPDATE: systemd file

Here are the details of the systemd service unit file that I have deployed on Ubuntu 16.04 box. This one works on every reboot.


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

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

[Install]
WantedBy=multi-user.target

1 Answers1

2

Try this:

#!upstart
description "Send Message on Startup"

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

Here's another option which should solve it. Basically wait till it responds to 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."
hookenz
  • 14,472
  • 23
  • 88
  • 143
  • Thank you for the quick response. I gave it a try and I had the same result, unfortunately. – rabidhamper7 May 02 '17 at 20:57
  • Do you have any other networks connected? you could also try putting a "sleep 5" in the script before sending with nc – hookenz May 02 '17 at 21:07
  • The sleep is what it needed! Thanks for your help! Add that to your answer and I'll accept it. – rabidhamper7 May 02 '17 at 21:16
  • Doesn't `ping -c 2` continue to ping until it gets a response? Why is the while loop necessary? – rabidhamper7 May 02 '17 at 21:24
  • @rabidhamper7 - that's why the -c 2. Get two successful replies then exit. – hookenz May 02 '17 at 21:29
  • Right, so the while loop isn't necessary for this? Or will the ping fail completely if it can't get a network connection? – rabidhamper7 May 02 '17 at 21:37
  • @rabidhamper7 - ping will never end without -c. And if there is no response the return code will not be zero. Should probably add a timeout like -W 5 or something also. – hookenz May 02 '17 at 21:52