Search code examples
network-programmingstartupubuntu-17.04

Run script on sturtup or do network sertup? Ubuntu17.04


Hello everybody,

That question might be silly, I tried to google, but nothing works for me.

So what I need is to run script on startup after interfaces eno1 and eno2 up and got their IPs:

#!/bin/sh
ip rule add from 172.28.1.0/24 pref 200 lookup 201
ip route add default via 172.28.1.1 dev eno1 table 201
ip rule add from 172.28.2.0/24 pref 200 lookup 202
ip route add default via 172.28.2.1 dev eno2 table 202
ip route add default nexthop via 172.28.1.1 dev eno1
ip route append default nexthop via 172.28.2.1 dev eno2
ip route flush cache
systemctl start apache2

This is to setup some network parameters. So I tried:

1. Add commands to /etc/rc.local

Not working, because I don't have /etc/rc.local file

2. Add this script as Startup Application

Not working assuming that that need a sudo rights(don't know how to run script with sudo on startup)

3.Add an init script (obsolete)

Not working too, I assume because of sudo rights as in previous case.

Comment: I do systemctl start apache2, because after system starts, apache2 exits with next error:

no listening sockets available, shutting down

EDIT: I created a service - net_conf.service:

[Unit]
After=network-online.target

[Service]
ExecStart=/network_configuration/conf.sh

[Install]
WantedBy=default.target

And my script is conf.sh you can see below.

The result of it next(systemctl status net_conf.service):

Oct 10 23:02:09 loraserver conf.sh[1848]: RTNETLINK answers: Network is unreachable
Oct 10 23:02:09 loraserver conf.sh[1848]: RTNETLINK answers: File exists
Oct 10 23:02:09 loraserver conf.sh[1848]: RTNETLINK answers: Network is unreachable
Oct 10 23:02:09 loraserver conf.sh[1848]: RTNETLINK answers: Network is unreachable
Oct 10 23:02:09 loraserver conf.sh[1848]: RTNETLINK answers: Network is unreachable
Oct 10 23:02:09 loraserver conf.sh[1848]: Job for apache2.service failed because the control process exited with error code.
Oct 10 23:02:09 loraserver conf.sh[1848]: See "systemctl status apache2.service" and "journalctl -xe" for details.

EDIT2: Also tried to add -s -q --timeout=30 and with 120 argument to the Exes start. Doesn't work. Any suggestions?

Any help is appreciated, Thank you in advance.

Best regards, Oleg Somov


Solution

  • With newer versions of Linux (including Ubuntu since version 15.04 I think) systemd is the system for starting system services. In general with systemd you create a service file that will run your script(s) at the correct time in the OS's boot process.

    You may find this document helpful:

    https://wiki.ubuntu.com/SystemdForUpstartUsers

    It details how to create a new service, and how to set it to run on startup (or at some other time).