0

I'm trying to send a Slack notification of a server's IP address on startup. I'm running Ubuntu 18.04. The script works fine when ran from command line (creates a log file & send the Slack message). It's placed at /etc/init.d/iptoslack.sh, and I've set chmod 755 and run

sudo update-rc.d iptoslack.sh defaults

and added a delay to the script so the internet connection would have time to establish. However, even the log file doesn't get created on boot. Scripts in init.d shouldn't require login either, right? Can this be a permission issue (aren't init.d scripts run as root?)

/etc/init.d/iptoslack.sh:

#!/bin/bash

script()
{
  ifconfig > /home/myname/startlog.log
  sleep 30 # wait a moment to give the internet connection time to establish
  ip="$(ifconfig | sed -n 2p)"
  text="Intra server rebooted, ifconfig returned $ip"
  curl -X POST -H 'Content-type: application/json' --data "{'text':'$text'}" 'https://hooks.slack.com/services/asdf/qwer/fghj' >> /home/myname/startlog.log
}

script &
Perdex
  • 130
  • 1
  • 12
  • maybe [opqdonut](https://stackoverflow.com/questions/8339555/how-to-run-a-script-at-the-start-up-of-ubuntu)'s answer helps you out. I don't see your mistake, I'm just guessing. Maybe `defaults` specifies a wrong runlevel. Also I think `init.d` is using `/bin/sh`, not `/bin/bash`. However, are you sure you want your script to be executed on startup? There's also the [possibility](https://askubuntu.com/questions/258580/how-to-run-a-script-depending-on-internet-connection#258585) to run this script every time the interface comes up. – Marco Feb 14 '19 at 10:29
  • The point is that I'm installing the server at home and will just carry the hard drive to the actual server. There's no screen handily available on that end so I'll need the IP to ssh into it. (I don't control the network so this was the quickest solution I came up with, other than it not working...) – Perdex Feb 14 '19 at 10:32
  • Ah I see, but that sounds like you want to do dynamic DNS? There are clients for that. No need to write an own script, plus you never need to check the IP again. Just remember the domain. Here are some [clients](https://wiki.ubuntuusers.de/DDNS-Clients/) and at the bottom of that page there's a list of free DNS providers. afraid.org is my personal favorite btw :) – Marco Feb 14 '19 at 10:37
  • Ok adding it to `/etc/network/if-up.d` works for me. Thanks! The original question still remains though... – Perdex Feb 14 '19 at 11:05

0 Answers0