3

I am using Debian 8 and have configured a script and put it under /etc/init.d directory as below. Basically, I want to open ssh tunnels on startup.

#! /bin/sh
# /etc/init.d/open_tunnels

### BEGIN INIT INFO
# Provides:          open_tunnels
# Required-Start:    $network $syslog $remote_fs
# Required-Stop:     $remote_fs $syslog 
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

case "$1" in
  start)
    echo "Running open_tunnels..."
    <ssh command>
    ;;
  stop)
    killall open_tunnels
    ;;
  *)
    echo "Usage: /etc/init.d/open_tunnels start"
    exit 1
    ;;
esac


exit 0

Then I added the service with

insserv open_tunnels

But, when I start the system I got this on syslog:

open_tunnels[522]: ssh: connect to host <IP> port 22: Network is unreachable

I am sure the ssh command works well because I use it on my system (after login). I just want it to be executed on startup.

Is there anything I am missing here?

  • Have you checked this?: thegeekstuff.com/2012/03/lsbinit-script – coffeMug Jun 20 '16 at 13:48
  • @coffeMug, Yes, I have. I just added the 'networking' close to $network and it didn't work. I think there is something more I have to do in order to make sure my networking device is up and running before executing the script. – bpinhosilva Jun 20 '16 at 13:56
  • Did you also add K and S entries in the run-levels? update-rc.d filename defaults – coffeMug Jun 20 '16 at 14:10
  • Actually, I tried this command but I saw that now Debian 8 does that operation using insserv instead of update-rc.d. – bpinhosilva Jun 20 '16 at 14:13
  • Debian 8 is using systemd now; try to rewrite it as a systemd service. – Martin von Wittich Jun 20 '16 at 14:47
1

Read https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/ for an exhaustive treatment of the matter. You main problem is this:

What precisely is required for $network is not obvious and can be different things depending on local configuration.

Systemd gives you better tools as detailed on the above link. Basically, you'll want to put

[Unit]
Require = network-online.target
After = network-online.target

in your custom service file and hope that your network management service provides a well-behaving network-online.target.

If you don't want to replace your init script with a systemd service file, create the /etc/systemd/system/open_tunnels.service.d directory and put the above 3 lines into any file (with .conf extension) in that directory. This will augment the service description autogenerated by the SysV init compatibility layer of systemd, as explained in man systemd.unit.

  • Ok, but this solution works if I write the script with systemd pointed by @Martin von Wittich, right? The syntax is a little different from what I am trying to do. – bpinhosilva Jun 21 '16 at 13:36
  • I don't get your note about "the syntax", but writing a custom service file would be the best option. Failing that you can try the service.d method I've just added to my answer. – Ferenc Wágner Jun 21 '16 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.