Linux shell implementation daemon script

Time:2022-5-11

Embedded beginners, upload code for the first time. Yesterday, I did a guard of udhcpd and udhcpc. At present, I can only imitate and write with shell. What else can I do?

#! /bin/sh
#The process name can be modified
PRO_NAME=udhcpc
WLAN=ra0
 
while true ; do
 
#Get $pro with PS_ Number of name processes
  NUM=`ps aux | grep ${PRO_NAME} | grep -v grep |wc -l`
#  echo $NUM
#Less than 1, restart the process
  if [ "${NUM}" -lt "1" ];then
    echo "${PRO_NAME} was killed"
    ${PRO_NAME} -i ${WLAN}
#Greater than 1, kill all processes and restart
  elif [ "${NUM}" -gt "1" ];then
    echo "more than 1 ${PRO_NAME},killall ${PRO_NAME}"
    killall -9 $PRO_NAME
    ${PRO_NAME} -i ${WLAN}
  fi
#Kill zombie process
  NUM_STAT=`ps aux | grep ${PRO_NAME} | grep T | grep -v grep | wc -l`
 
  if [ "${NUM_STAT}" -gt "0" ];then
    killall -9 ${PRO_NAME}
    ${PRO_NAME} -i ${WLAN}
  fi
done
 
exit 0

The above is the whole content of this article. I hope it can be helpful for you to be familiar with Linux shell scripts.