WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: lldpd not available for 16.x  (Read 1548 times)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 12235
Re: lldpd not available for 16.x
« Reply #15 on: August 24, 2025, 10:37:24 AM »
Hi orneh24
I realized there were a couple of items that needed fixing, so
I made some updates:
Code: [Select]
#!/bin/sh

if [ `/usr/bin/id -u` -ne 0 ]
then
echo -e "\nNeed to run as root or using sudo.\n"
exit 1
fi

NAME="lldpd"
DAEMON="/usr/local/sbin/$NAME"
DAEMON_ARGS=""

RUNNING=$(ps aux | grep -v "grep" | grep "$DAEMON")

start()
{
[ -n "$RUNNING" ] && echo -e "\n$NAME is already running.\n" && exit 2
start-stop-daemon -S -x "$DAEMON" -- "$DAEMON_ARGS"
}

stop()
{
start-stop-daemon -K -x "$DAEMON" 2>/dev/null
rm -rf /var/run/"$NAME"*
}



status()
{
[ -n "$RUNNING" ] && echo -e "\n$NAME is running.\n" && exit 0
echo -e "\n$NAME is not running.\n"
exit 1
}

case $1 in
start) start
;;
stop) stop
;;
status) status
;;
restart) stop; start
;;
*) echo -e "\n$0 [start|stop|restart|status]\n"
;;
esac

Added test for root.
Added test for running in start().
Simplified status().

I've attached the updated version here.

Download the attached file and make it executable:
Code: [Select]
chmod 774 lldpd
Copy it to  /usr/local/etc/init.d/  and test it out.
Like most (all?) init.d scripts, run it using sudo.
Please test it and let me know if there are any problems or changes required.
« Last Edit: August 25, 2025, 09:04:25 PM by Rich »

Offline orneh24

  • Newbie
  • *
  • Posts: 10
Re: lldpd not available for 16.x
« Reply #16 on: August 24, 2025, 11:07:03 PM »
Awesome, thanks again Rich  :D
Start/Stop/Status all works flawlessly, although Restart seems to fail;

Code: [Select]
tc@box:~$ sudo /usr/local/etc/init.d/lldpd start
tc@box:~$ sudo /usr/local/etc/init.d/lldpd status

lldpd is running.

tc@box:~$
tc@box:~$ sudo /usr/local/etc/init.d/lldpd restart
stopped /usr/local/sbin/lldpd (pid 1901 1899)

lldpd is already running.

tc@box:~$
tc@box:~$ sudo /usr/local/etc/init.d/lldpd status

lldpd is not running.


Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 12235
Re: lldpd not available for 16.x
« Reply #17 on: August 25, 2025, 12:21:10 AM »
Hi orneh24
... although Restart seems to fail; ...
I think see the issue. The  RUNNING  variable was not updated in stop():
Code: [Select]
#!/bin/sh

if [ `/usr/bin/id -u` -ne 0 ]
then
echo -e "\nNeed to run as root or using sudo.\n"
exit 1
fi

NAME="lldpd"
DAEMON="/usr/local/sbin/$NAME"
DAEMON_ARGS=""

RUNNING=$(ps aux | grep -v "grep" | grep "$DAEMON")

start()
{
[ -n "$RUNNING" ] && echo -e "\n$NAME is already running.\n" && exit 2
start-stop-daemon -S -x "$DAEMON" -- "$DAEMON_ARGS"
}

stop()
{
start-stop-daemon -K -x "$DAEMON" 2>/dev/null
RUNNING=$(ps aux | grep -v "grep" | grep "$DAEMON")
[ -n "$RUNNING" ] && echo -e "\n$NAME was not stopped.\n" && exit 3
rm -rf /var/run/"$NAME"*
}



status()
{
[ -n "$RUNNING" ] && echo -e "\n$NAME is running.\n" && exit 0
echo -e "\n$NAME is not running.\n"
exit 1
}

case $1 in
start) start
;;
stop) stop
;;
status) status
;;
restart) stop; start
;;
*) echo -e "\n$0 [start|stop|restart|status]\n"
;;
esac

Please see if the attached works any better.
« Last Edit: August 25, 2025, 09:04:44 PM by Rich »

Offline orneh24

  • Newbie
  • *
  • Posts: 10
Re: lldpd not available for 16.x
« Reply #18 on: August 25, 2025, 06:29:22 PM »
Testing with the updated script;

When daemon is currently running, and restart is initiated:
Code: [Select]
root@box:/# ./usr/local/etc/init.d/lldpd status

lldpd is running.

root@box:/# ./usr/local/etc/init.d/lldpd restart
stopped /usr/local/sbin/lldpd (pid 1791 1789)

lldpd was not stopped.

root@box:/# ps -C lldpd
  PID TTY          TIME CMD
root@box:/#

So it looks like the variable is still not updated correctly.

If the daemon is currently stopped, and restart is initiated:
Code: [Select]
root@box:/# ./usr/local/etc/init.d/lldpd status

lldpd is not running.

root@box:/# ./usr/local/etc/init.d/lldpd restart
no /usr/local/sbin/lldpd found; none killed
root@box:/# ./usr/local/etc/init.d/lldpd status

lldpd is running.

root@box:/# ps -C lldpd
  PID TTY          TIME CMD
 1853 ?        00:00:00 lldpd
 1855 ?        00:00:00 lldpd
root@box:/#

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 12235
Re: lldpd not available for 16.x
« Reply #19 on: August 25, 2025, 09:03:55 PM »
Hi orneh24
OK, I installed lldpd on one of my machines and tracked down the problem.
When  start-stop-daemon  stops a process, it sends it a terminate signal and
then returns control to whatever called it. Meanwhile, the process still needs
to receive that signal and act upon it. So we had a race condition. I added a
loop that will wait up to 3 seconds for the process to terminate. On my machine
the delay loop dropped out after 0.6 seconds:
Code: [Select]
#!/bin/sh

if [ `/usr/bin/id -u` -ne 0 ]
then
echo -e "\nNeed to run as root or using sudo.\n"
exit 1
fi

NAME="lldpd"
DAEMON="/usr/local/sbin/$NAME"
DAEMON_ARGS=""

RUNNING=$(ps aux | grep -v "grep" | grep "$DAEMON")

start()
{
[ -n "$RUNNING" ] && echo -e "\n$NAME is already running.\n" && exit 2
start-stop-daemon -S -x "$DAEMON" -- "$DAEMON_ARGS"
}

stop()
{
start-stop-daemon -q -K -x "$DAEMON" 2>/dev/null
for DELAY in $(seq 1 30)
do
RUNNING=$(ps aux | grep -v "grep" | grep "$DAEMON")
[ -z "$RUNNING" ] && break
done
[ -n "$RUNNING" ] && echo -e "\n$NAME was not stopped.\n" && exit 3
rm -rf /var/run/"$NAME"*
}



status()
{
[ -n "$RUNNING" ] && echo -e "\n$NAME is running.\n" && exit 0
echo -e "\n$NAME is not running.\n"
exit 1
}

case $1 in
start) start
;;
stop) stop
;;
status) status
;;
restart) stop; start
;;
*) echo -e "\n$0 [start|stop|restart|status]\n"
;;
esac

Please check if I finally got it right.

Offline orneh24

  • Newbie
  • *
  • Posts: 10
Re: lldpd not available for 16.x
« Reply #20 on: August 25, 2025, 11:07:25 PM »
Hey Rich,

..Please check if I finally got it right..
.. You did  :)

Quote
root@box:/home/tc# ps -C lldpd
  PID TTY          TIME CMD
 1639 ?        00:00:00 lldpd
 1641 ?        00:00:00 lldpd
root@box:/home/tc# /usr/local/etc/init.d/lldpd status

lldpd is running.

root@box:/home/tc# /usr/local/etc/init.d/lldpd restart
root@box:/home/tc#
root@box:/home/tc# /usr/local/etc/init.d/lldpd status

lldpd is running.

root@box:/home/tc# ps -C lldpd
  PID TTY          TIME CMD
 1747 ?        00:00:00 lldpd
 1749 ?        00:00:00 lldpd
root@box:/home/tc#


Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 771
Re: lldpd not available for 16.x
« Reply #21 on: August 26, 2025, 01:42:05 AM »
Why not like another deamons SysV-init-skript rc files make a pid file with the pid number, and kill that number.
Something like this:
Code: [Select]
#!/bin/sh
#
# Simple init script to start/stop a daemon with a PID file

DAEMON="/usr/local/bin/mydaemon"   # executable
PIDFILE="/var/run/mydaemon.pid"    # pid file
NAME="mydaemon"

start() {
    if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") 2>/dev/null; then
        echo "$NAME is already running"
        return 0
    fi
    echo "Starting $NAME..."
    $DAEMON &
    echo $! > "$PIDFILE"
    echo "$NAME started with PID $(cat $PIDFILE)"
}

stop() {
    if [ -f "$PIDFILE" ]; then
        PID=$(cat "$PIDFILE")
        if kill -0 "$PID" 2>/dev/null; then
            echo "Stopping $NAME..."
            kill "$PID"
            rm -f "$PIDFILE"
            echo "$NAME stopped"
        else
            echo "Process not running, removing stale PID file"
            rm -f "$PIDFILE"
        fi
    else
        echo "$NAME is not running"
    fi
}

status() {
    if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") 2>/dev/null; then
        echo "$NAME is running with PID $(cat $PIDFILE)"
        return 0
    else
        echo "$NAME is not running"
        return 1
    fi
}

restart() {
    stop
    sleep 1
    start
}

case "$1" in
    start) start ;;
    stop) stop ;;
    restart) restart ;;
    status) status ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

exit 0
« Last Edit: August 26, 2025, 01:48:20 AM by patrikg »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 12235
Re: lldpd not available for 16.x
« Reply #22 on: August 26, 2025, 02:28:18 PM »
Hi patrikg
Why not like another deamons SysV-init-skript rc files make a pid file with the pid number, ...
Here's what happens when you background lldpd:
Code: [Select]
tc@box:~/lldpd$
tc@box:~/lldpd$
tc@box:~/lldpd$
tc@box:~/lldpd$ sudo /usr/local/sbin/lldpd &
tc@box:~/lldpd$
[1]+  Done                       sudo /usr/local/sbin/lldpd
tc@box:~/lldpd$ echo $!
5186
tc@box:~/lldpd$ ps aux | grep -v "grep" | grep /usr/local/sbin/lldpd
 5187 _lldpd   /usr/local/sbin/lldpd
 5189 _lldpd   /usr/local/sbin/lldpd
tc@box:~/lldpd$
It launches 2 instances of itself and exits. The PID you get back is
for the original instance you launched, which already exited.

Also, lldpd creates its own PID file:
Code: [Select]
tc@box:~/lldpd$ cat /var/run/lldpd.pid
5187
tc@box:~/lldpd$

Quote
... and kill that number. ...
start-stop-daemon stop  sends kill signals to all copies of lldpd, regardless
of how many copies are currently running.

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 771
Re: lldpd not available for 16.x
« Reply #23 on: August 26, 2025, 03:24:45 PM »
Ohh it's forking it self to two, didn't see that, sorry.

But if it's making it's own pid file it's should be the one you need, or am wrong ?
Should it be two pid numbers in that file ?

Or can you just kill the main process to kill the child.(sorry for this type of language)
« Last Edit: August 26, 2025, 03:26:46 PM by patrikg »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 12235
Re: lldpd not available for 16.x
« Reply #24 on: August 26, 2025, 04:10:46 PM »
Hi patrikg
... Or can you just kill the main process to kill the child.(sorry for this type of language)
In a perfect world, yes. But if something goes wrong, it's probably
safer to send kill signals to all instances.