0

I'm trying to set up a System-V init script on my Raspbian, just to send an HTTP POST request to a public web server (99,9% uptime), reporting that the Raspbian has just been started up (or rebooted). So far, I've done the following:

Edited /etc/init.d/notify-startup (initial test T0) (showing only relevant lines):

...
# Required-Start:       $remote_fs $syslog $time
# Required-Stop:        $remote_fs $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
...
case "$1" in
  start)
    logger -i "Starting Notify-Startup" || true
    /opt/notify-startup/bin/notify-startup.sh &
...

And /opt/notify-startup/bin/notify-startup.sh:

#!/bin/sh

# First.
curl --insecure --include --request POST \
    --url https://example.com/notify-startup.php \
    --header "Content-Type: application/x-www-form-urlencoded" \
    --data-urlencode "id=0" \
    --data-urlencode "count=1" \
    --data-urlencode "message=Starting up (1)" > /dev/null 2>&1
sleep 180
# Second.
curl --insecure --include --request POST \
    --url https://example.com/notify-startup.php \
    --header "Content-Type: application/x-www-form-urlencoded" \
    --data-urlencode "id=0" \
    --data-urlencode "count=2" \
    --data-urlencode "message=Starting up (2)" > /dev/null 2>&1

exit 0

Run update-rc.d to create System-V style links under /etc/rc?.d:

$ sudo update-rc.d notify-startup defaults # Command [C1]

After this execution, my symlinks under /etc/rc?.d are the following:

/etc/rc0.d/K01notify-startup
/etc/rc1.d/K01notify-startup
/etc/rc2.d/S02notify-startup
/etc/rc3.d/S02notify-startup
/etc/rc4.d/S02notify-startup
/etc/rc5.d/S02notify-startup
/etc/rc6.d/K01notify-startup

Then I reboot my Raspbian.

Result is that the first HTTP POST request does not reach the public web server but the second one actually does, and I'd like both of them would do.

It's important to say that Ethernet interface and DNS resolver must be operative so that my HTTP POST request could reach the public web server. So I've tried the following combinations of LSB facility names [1] on my script with no success until now:

Test [T1]:

# Required-Start:       $network $remote_fs $syslog $time

Test [T2]:

# Required-Start:       $named $remote_fs $syslog $time

I also have noticed that, after re-running update-rc.d [C1], the symlinks under /etc/rc?.d are not re-numbered in order to satisfy the dependencies on facility names configured on tests [T1] and [T2].

My question is: Is there any way to ensure that my startup script gets run just after DNS resolution is available to my Raspbian system, by using facility name dependencies or by any other programmatic way (I would prefer the former)? Am I doing something wrong with command [C1] so that my startup script execution order does not get updated and does not ensure DNS resolution and network availability?

PS: I already know that this simple task could be achieved well by using just a @reboot crontab job, but I need it to be a System-V startup script as I would be upgrading it to add more and more functionality over time.

[1] https://refspecs.linuxfoundation.org/LSB_1.2.0/gLSB/facilname.html

  • 1
    Why don't you use systemd? It's easy to achieve what you want with it. – jake Jan 26 at 1:58
  • Please read systemd - Compatibility with SysV before doing further debugging and have a look at man systemd-sysv-generator. You shouldn't ride a dead horse. – Ingo Jan 26 at 12:35
  • Thanks, @jake and @Ingo, for your suggestion. I've found this file: /run/systemd/generator.late/notify-startup.service, whose first line says: # Automatically generated by systemd-sysv-generator. Do you think the contents of this file would be a good starting point to solve my problem using the systemd approach? – Pestro Jan 27 at 20:23
  • Yes! Take it. Delete everything not needed. Add an [Install] section (for example WantedBy=default.target) and something like After=network-online.target to the [Unit] section. Then you can enable it by running systemctl daemon-reload and systemctl enable <your>.service. Put your service unit file in /etc/systemd/system/. – jake Jan 27 at 20:45

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.