Keep-alive script for SSHD?

I’m using a keep-alive script for VNC which works, but I tried the same script for SSHD which doesn’t. Does anyone have any tips on how to get it working? Here’s the script I’m using. The script itself restarts, but it doesn’t restart the actual SSHD binary. The binary has the same PID whenever I check it in Terminal with “ps”.

#!/bin/bash
PROGRAM=/boot/home/config/non-packaged/bin/sshd
$PROGRAM &
PID=$!
echo $PID > /boot/home/config/settings/sshd.pid
sleep 7m
for line in $(cat /boot/home/config/settings/sshd.pid)
do
echo
done
kill $line
launchsshd &

Could you explain us, why do you need this?

1 Like

Because apparently there is some bug in the network stack related to long uptimes, I think. But of course nobody takes the time to fully evalulate such bugs and write a proper report so we can fix them…

Come on, that’s really a bit unfair to all the people in the community that do report bugs all the time, don’t you think? :wink:

Yeah, it’s the same issue as in this thread regarding VNC connection dropouts.

The VNC script which works is below ( I set it to restart every 6 minutes because the dropouts are frequent):

#!/bin/bash
PROGRAM=/boot/home/config/bin/vncserver
$PROGRAM &
PID=$!
echo $PID > /boot/home/config/settings/vnc.pid
sleep 6m
for line in $(cat /boot/home/config/settings/vnc.pid)
do
echo
done
kill $line
launchvnc &

Because SSHD drops out as well, I tried the same script as above for SSHD, but the script doesn’t restart it. It does start SSHD on initial boot-up (I have the VNC and SSHD scripts to launch on boot-up), but running “ps” later in Terminal always shows SSHD as the same PID, even though the script’s own PID changes. Eventually the SSHD connection will no longer work, at random intervals. I put a copy of sshd in /boot/home/config/non-packaged. Here’s the script:

#!/bin/bash
PROGRAM=/boot/home/config/non-packaged/bin/sshd
$PROGRAM &
PID=$!
echo $PID > /boot/home/config/settings/sshd.pid
sleep 7m
for line in $(cat /boot/home/config/settings/sshd.pid)
do
echo
done
kill $line
launchsshd &

I suppose I’d say the same thing I did last time. The output from the script will tell you what’s going wrong.

Hi Donn,

Found it :slight_smile: How would I implement that? I’m just an end-user here, not too savvy with scripts and such. Is it a bash file?

" You’ll be able to see for yourself what’s actually going wrong, if you create a log file where you can look at errors encountered during execution. The script itself can do that, if you like, with the “exec” shell command - “exec > /tmp/logfile 2>&1” The exec command normally executes a file in the same process, terminating the shell, but with no file mentioned it just operates on the current redirections. The above redirects standard output unit 1 to /tmp/logfile, and makes error output unit 2 a duplicate of that unit.

The vnc.pid file is kind of wasted on some unnecessary shell folderol here, but would give the script the ability to deal with an existing vncserver. Something like "

PIDFILE=/boot/home/config/settings/vnc.pid

# function to output first "ps" column on line(s) where 3rd-from-last column
# matches the function parameter.
pidexe () { ps | awk -vPID=$1 '$(NF-3) == PID { print $1 }' ; }

# function to kill $PROGRAM when it's running under the specified PID
killvnc () {
   case $(pidexe $1) in
   $PROGRAM)
       echo VNC server already running, PID $1 >&2
       kill $1
       ;;
   esac
}

if pid=$(cat $PIDFILE) && kill -0 $pid
then killvnc $pid
fi

$PROGRAM &
pid=$!
echo $pid > $PIDFILE
...

File or whatever. Commands interpreted by the shell, same whether you type them in at the interactive shell command line in a Terminal window, or a shell script. Experiment until it’s clear. Try commands that may have error output, like “ls” with a non-existent file, or a command that doesn’t exist. The particular “exec” statement I mentioned there is intended for a shell script, because it affects the standard I/O for everything subsequently invoked by the shell.