LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I execute inadyn automatically during boot? (http://www.linuxquestions.org/questions/linux-software-2/how-do-i-execute-inadyn-automatically-during-boot-541367/)

ashesh0326 03-28-2007 12:49 PM

How do I execute inadyn automatically during boot?
 
Friends,
I'm using dyndns.org to maintain my domain, and I use inadyn to keep my ip updated at that site (in case my link fails and my ip gets changed.)
I need to execute this at the command line after every reboot: inadyn -u <username> -p <password> -a ashesh.homeip.net
Given that I'm using FC 6, is there some place I can add this line so that it is executed automatically during bootup?
I'll really appreciate your help guys.
Thanks in advance,
Peace.

Wynd 03-28-2007 01:14 PM

Make a file called /etc/init.d/inadyn. Put this in it:
Code:

#!/bin/bash

case "$1" in
    start)
        inadyn -u <username> -p <password> -a ashesh.homeip.net
        ;;
    stop)
        (whatever commands you would use to stop the service)
        ;;
    reload|restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 start|stop|restart|reload"
        exit 1
esac
exit 0

Then run 'chkconfig inadyn' and 'service inadyn start'

Recommended reading: Create a Startup Script

ashesh0326 03-29-2007 07:23 AM

Thank you!! It worked!

vpablos 02-06-2010 03:47 AM

Inadyn does not work always by itself ... :-(
 
The problem with this solution is that inadyn does not detect if network is up, and sometimes it updates too much times and server disables your dynamic dns account.
This script works most of the times. It uses dhcp to know when ip has been updated.

#!/bin/sh

# FILE: dhcp3/dhclient-exit-hooks.d/zzz_public_ip

username=*fill in*
password=*fill in*
domain=*fill in*

dns_ip=`host "$domain" |sed -e 's/[A-Za-z. \-]* //'`
actual_ip=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`

function test_network_and_ip_on_dns() {
# Some times ping is not allowed :-(
# case `ping -qnc 1 google.com 2>&1` in
# *'100% packet loss'*)
# logger -t publicIP “The network is DOWN.”
# exit 0
# ;;
# esac

NETSTATUS=`wget -O - www.google.com 2>&1 | grep "Network is unreachable"`
# echo "NETSTATUS: $NETSTATUS."
if [ -z "$NETSTATUS" ] || [ "" == "$NETSTATUS" ]; then
test_ip_on_dns
else
logger -t publicIP “Network is DOWN.”
exit 0
fi
}

function test_ip_on_dns() {
if [ "$dns_ip" == "$actual_ip" ]; then
logger -t publicIP "Public IP is $actual_ip"
else
logger -t publicIP "Updating ip for domain $domain with username $username"
/usr/sbin/inadyn --iterations 1 -u $username -p $password -a $domain
logger -t publicIP "Public IP is $actual_ip"
fi
}

if [ -z "$reason" ]; then
test_network_and_ip_on_dns
exit 0
fi

case "$reason" in
MEDIUM|ARPCHECK|ARPSEND|NBI|BOUND|RENEW|REBIND)
logger -t publicIP "Updating public IP. Reason: $reason"
test_network_and_ip_on_dns
;;

EXPIRE|FAIL|TIMEOUT|PREINIT|REBOOT|STOP|RELEASE|*)
logger -t publicIP "Not updating. Reason: $reason"
;;
esac

vpablos 02-09-2010 03:07 AM

Improved script.
 
The previous script some times hangs, specially when no internet connection is available (Network is unreachable or unable to resolve host address). This one takes care of these problems.

#!/bin/sh

# FILE: dhcp3/dhclient-exit-hooks.d/zzz_public_ip

username=*fill in*
password=*fill in*
domain=*fill in*

function test_network_and_ip_on_dns() {
# case `ping -qnc 1 google.com 2>&1` in
# *'100% packet loss'*)
# logger -t publicIP “The network is DOWN.”
# exit 0
# ;;
# esac

TEST=`wget --timeout=10 --tries=1 -O - www.google.com 2>&1`
TEST_1=`echo $TEST | grep "Network is unreachable"`
TEST_2=`echo $TEST | grep "unable to resolve host address"`
# echo "NETSTATUS: $NETSTATUS."

echo "test: $TEST"
echo "test_1: $TEST_1"
echo "test_2: $TEST_2"

# Check that network is up.
if [ ! -z "$TEST_1" ] || [ ! "" == "$TEST_1" ] || [ ! -z "$TEST_2" ] || [ ! "" == "$TEST_2" ]; then
logger -t publicIP “Network is DOWN.”
exit 0
fi

# Only if none of them is set then network is up.
test_ip_on_dns
}

function test_ip_on_dns() {
dns_ip=`host "$domain" |sed -e 's/[A-Za-z. \-]* //'`
actual_ip=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`


if [ "$dns_ip" == "$actual_ip" ]; then
logger -t publicIP "Public IP is $actual_ip"
else
logger -t publicIP "Updating ip for domain $domain with username $username"
/usr/sbin/inadyn --iterations 1 -u $username -p $password -a $domain
logger -t publicIP "Public IP is $actual_ip"
fi
}

if [ -z "$reason" ]; then
test_network_and_ip_on_dns
exit 0
fi

case "$reason" in
MEDIUM|ARPCHECK|ARPSEND|NBI|BOUND|RENEW|REBIND)
logger -t publicIP "Updating public IP. Reason: $reason"
test_network_and_ip_on_dns
;;

EXPIRE|FAIL|TIMEOUT|PREINIT|REBOOT|STOP|RELEASE|*)
logger -t publicIP "Not updating. Reason: $reason"
;;
esac

trendyserial 11-07-2011 05:50 PM

Init script for Inadyn
 
First of all you have to create (or edit) inadyn configuration file:

/etc/inadyn.conf

this way

Code:

# Basic configuration file for inadyn
#
# /etc/inadyn.conf
update_period_sec 600 # Check for a new IP every 600 seconds
username yourusername
password yourpassword
dyndns_system dyndns@dyndns.org
alias yourdomainname
background


Now put this code in /etc/init.d/inadyn

Code:

#!/bin/bash
case "$1" in
    start)
        if [ -f /tmp/inadyn.pid ]; then
                PID=$(cat /tmp/inadyn.pid)
                kill -0 ${PID} &>/dev/null
                if [ $? = 0 ]; then
                        echo "Inadyn is already running."
                else
                        /usr/sbin/inadyn
                        pidof inadyn > /tmp/inadyn.pid
                        PID=$(cat /tmp/inadyn.pid)
                        kill -0 ${PID} &>/dev/null
                        if [ $? = 0 ]; then
                                echo "Inadyn started succesfully."
                        else
                                echo "Error starting Inadyn"
                        fi
                fi
        else
                /usr/sbin/inadyn
                pidof inadyn > /tmp/inadyn.pid
                PID=$(cat /tmp/inadyn.pid)
                kill -0 ${PID} &>/dev/null
                if [ $? = 0 ]; then
                        echo "Inadyn started succesfully."
                else
                        echo "Error starting Inadyn"
                fi
        fi
        ;;
    stop)
        if [ -f /tmp/inadyn.pid ];then
                PID=$(cat /tmp/inadyn.pid)
                kill -0 ${PID} &>/dev/null
                if [ $? = 0 ]; then
                        /bin/kill ${PID}
                        kill -0 ${PID} &>/dev/null
                        if [ $? = 1 ]; then
                                echo "Inadyn stopped succesfully."
                        else
                                echo "Error stopping Inadyn"
                        fi
                else
                        echo "Inadyn is already stopped."
                fi
        else
                echo "Inadyn is already stopped."
        fi
        ;;
    reload|restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 start|stop|restart|reload"
        exit 1
esac
exit 0

Then create these two links to start and stop service at system boot and shutdown:
ln -s /etc/init.d/inadyn /etc/rc2.d/S03inadyn
ln -s /etc/init.d/inadyn /etc/rc0.d/K03inadyn

jepjep 04-06-2013 03:18 PM

runlevels and script
 
Hello trendyserial,

Thanks for the excellent solution.
I would add to it that "/etc/init.d/inadyn" script should be executable:

Code:

# chmod a+x /etc/init.d/inadyn
and that instead of creating links manually, the "former" way to setup a script among runlevels would be (at least in Debian?):

Code:

# update-rc.d inadyn defaults
update-rc.d: using dependency based boot sequencing
insserv: warning: script 'inadyn' missing LSB tags and overrides
vcasa:/etc# find ./rc* -iname "*inadyn*"
./rc0.d/K01inadyn
./rc1.d/K01inadyn
./rc2.d/S20inadyn
./rc3.d/S20inadyn
./rc4.d/S20inadyn
./rc5.d/S20inadyn
./rc6.d/K01inadyn




Quote:

Originally Posted by trendyserial (Post 4518378)
First of all you have to create (or edit) inadyn configuration file:

/etc/inadyn.conf

this way

Code:

# Basic configuration file for inadyn
#
# /etc/inadyn.conf
update_period_sec 600 # Check for a new IP every 600 seconds
username yourusername
password yourpassword
dyndns_system dyndns@dyndns.org
alias yourdomainname
background


Now put this code in /etc/init.d/inadyn

Code:

#!/bin/bash
case "$1" in
    start)
        if [ -f /tmp/inadyn.pid ]; then
                PID=$(cat /tmp/inadyn.pid)
                kill -0 ${PID} &>/dev/null
                if [ $? = 0 ]; then
                        echo "Inadyn is already running."
                else
                        /usr/sbin/inadyn
                        pidof inadyn > /tmp/inadyn.pid
                        PID=$(cat /tmp/inadyn.pid)
                        kill -0 ${PID} &>/dev/null
                        if [ $? = 0 ]; then
                                echo "Inadyn started succesfully."
                        else
                                echo "Error starting Inadyn"
                        fi
                fi
        else
                /usr/sbin/inadyn
                pidof inadyn > /tmp/inadyn.pid
                PID=$(cat /tmp/inadyn.pid)
                kill -0 ${PID} &>/dev/null
                if [ $? = 0 ]; then
                        echo "Inadyn started succesfully."
                else
                        echo "Error starting Inadyn"
                fi
        fi
        ;;
    stop)
        if [ -f /tmp/inadyn.pid ];then
                PID=$(cat /tmp/inadyn.pid)
                kill -0 ${PID} &>/dev/null
                if [ $? = 0 ]; then
                        /bin/kill ${PID}
                        kill -0 ${PID} &>/dev/null
                        if [ $? = 1 ]; then
                                echo "Inadyn stopped succesfully."
                        else
                                echo "Error stopping Inadyn"
                        fi
                else
                        echo "Inadyn is already stopped."
                fi
        else
                echo "Inadyn is already stopped."
        fi
        ;;
    reload|restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 start|stop|restart|reload"
        exit 1
esac
exit 0

Then create these two links to start and stop service at system boot and shutdown:
ln -s /etc/init.d/inadyn /etc/rc2.d/S03inadyn
ln -s /etc/init.d/inadyn /etc/rc0.d/K03inadyn


Jokesterfr 05-30-2013 04:50 PM

Do not forget to set the --background parameter, also please match the LSB tags requirements:

Code:

#!/bin/bash

### BEGIN INIT INFO
# Provides:          inadyn
# Required-Start:    $remote_fs $syslog
# 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

inadyn=/usr/sbin/inadyn
conf=/etc/inadyn.conf

case "$1" in
    start)
        if [ -f /tmp/inadyn.pid ]; then
                PID=$(cat /tmp/inadyn.pid)
                kill -0 ${PID} &>/dev/null
                if [ $? = 0 ]; then
                        echo "Inadyn is already running."
                else
                        $inadyn --background --input_file $conf
                        pidof inadyn > /tmp/inadyn.pid
                        PID=$(cat /tmp/inadyn.pid)
                        kill -0 ${PID} &>/dev/null
                        if [ $? = 0 ]; then
                                echo "Inadyn started succesfully."
                        else
                                echo "Error starting Inadyn"
                        fi
                fi
        else
                $inadyn --background --input_file $conf
                pidof inadyn > /tmp/inadyn.pid
                PID=$(cat /tmp/inadyn.pid)
                kill -0 ${PID} &>/dev/null
                if [ $? = 0 ]; then
                        echo "Inadyn started succesfully."
                else
                        echo "Error starting Inadyn"
                fi
        fi
        ;;
    stop)
        if [ -f /tmp/inadyn.pid ];then
                PID=$(cat /tmp/inadyn.pid)
                kill -0 ${PID} &>/dev/null
                if [ $? = 0 ]; then
                        /bin/kill ${PID}
                        kill -0 ${PID} &>/dev/null
                        if [ $? = 1 ]; then
                                echo "Inadyn stopped succesfully."
                        else
                                echo "Error stopping Inadyn"
                        fi
                else
                        echo "Inadyn is already stopped."
                fi
        else
                echo "Inadyn is already stopped."
        fi
        ;;
    reload|restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 start|stop|restart|reload"
        exit 1
esac
exit 0



All times are GMT -5. The time now is 03:34 AM.