Escriba un script de shell para iniciar y detener el servicio rsync

prefacio

         Use el puerto de servicio de consultas de Linux para juzgar el inicio, la detención y la actualización del servicio de activación de condiciones de escritura


1. Concha

        Shell es un programa escrito en lenguaje C, que es un puente para que los usuarios usen Linux. Shell es tanto un lenguaje de comandos como un lenguaje de programación. Shell se refiere a un programa de aplicación que proporciona una interfaz a través de la cual los usuarios acceden a los servicios del kernel del sistema operativo. El sh de Ken Thompson es el primer shell de Unix, y Windows Explorer es un típico shell de interfaz gráfica.

2. Escribir secuencias de comandos de inicio y detención del servicio rsync

  • Verifique el comando de inicio del servicio según /usr/lib/systemd/system/rsyncd.service
[root@controller init.d]# cat /usr/lib/systemd/system/rsyncd.service
[Unit]
Description=fast remote file copy program daemon
ConditionPathExists=/etc/rsyncd.conf

[Service]
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"  #启动方法

Use el comando netstat -tunlp |grep rsync |wc -l para verificar si el servicio está habilitado, lo cual es conveniente para el juicio condicional en el script

[root@controller init.d]# cat rsync 
#!/bin/bash
status=`netstat -tunlp |grep rsync |wc -l`

if [ "$1" = "start" ]
  then
    /usr/bin/rsync --daemon
  if [ "$status" -lt 1 ]
    then 
      sleep 2
      echo "Rsync started !!"
  else
    echo "Rsync already Started!"
  fi
elif [ "$1" = "stop" ]
  then 
    if [ "$status" -gt 0 ]
      then
        killall rsync
        sleep 2
        echo "Rsync stoped !!"
    else
      echo "Get something wrong,please check the status"
    fi
elif [ "$1" = "restart" ]
  then
    killall rsync
    echo "Rsync stopping!!"
    sleep 2
    echo "Rsync stoped !!"
    if [ "$status" -lt 1 ]
      then
        /usr/bin/rsync --daemon
        echo "Rsync starting!!"
    else
      echo "Rsync already Started!"
    fi
    /usr/bin/rsync --daemon
else
  echo "Usage ./rsync {start|stop|restart}"
fi

3. Ejecute la prueba

[root@controller init.d]# ./rsync.sh 
Usage ./rsync {start|stop|restart}
[root@controller init.d]# netstat -ntpl |grep rsync
[root@controller init.d]# ./rsync.sh start
Rsync started !!
[root@controller init.d]# netstat -ntpl |grep rsync
tcp        0      0 127.0.0.1:873           0.0.0.0:*               LISTEN      8099/rsync          
[root@controller init.d]# ./rsync.sh stop
Rsync stoped !!
[root@controller init.d]# netstat -ntpl |grep rsync
[root@controller init.d]# ./rsync.sh restart
Rsync stopping!!
Rsync stoped !!
Rsync starting!!
[root@controller init.d]# netstat -ntpl |grep rsync
tcp        0      0 127.0.0.1:873           0.0.0.0:*               LISTEN      8161/rsync   

Supongo que te gusta

Origin blog.csdn.net/TttRark/article/details/131177430
Recomendado
Clasificación