First, before writing the script, you need to know about the theoretical knowledge and practical operation of rsync, ok to write a script.
Let's take a stroke of a stroke:
rsync start command:
rsync --daemon
Stop command:
kill rsync process ID
pkill rsync
killall rsync
Man of few words said on the script:
#!/bin/bash ##################################### # File Name: rsyncd.sh # Version: V1.0 # Author: wuhan # Created Time : 2018-10-14 13:19:53 # Description: rsync service script ##################################### . /etc/init.d/functions # Loaded first /etc/init.d/functions, we will need to be inside the base function lockfile="/var/lock/subsys/rsyncd" # Define a variable lock file rpfp="/var/run/rsyncd.pid" # Write a variable rsync port ID, to facilitate multiple references can be used in this script # Write a function called start start(){ #Function name rsync --daemon && >/dev/null #rsync service Start command to empty after a successful start retval=$? # Get Return value if [ $retval -eq 0 ] If the return value is equal to 0 # then # Then do the following action "rsync startup ok" /bin/true # Successful start with the action function of the output of the content and displays the ok touch $lockfile # Create a lock file return $retval # Returns the start command after the return value else #otherwise action "rsync startup fail" /bin/false # Output for the service failed to start in the display false return $retval # Also returns after the command to start the return value, easy troubleshooting be #drop out } # Write a stop command function called stop stop(){ if test -s "$rpfp" If the port ID # file length is not zero, then true, perform the following operations then rsyncd_pid=`cat $rpfp` # Define a variable port ID number if (kill -0 $rsyncd_pid &>/dev/null) # If the port ID number exists, and outputs it to the empty then # Do the following kill $rsyncd_pid # Kill rsync service by process ID retval=$? # Get Return value if [ $retval -eq 0 ] # If the return value is equal to 0, the following command then action "rsync stop ok" /bin/true # Output service stops successfully, display ok rm -fr $lockfile # If you kill the rsync service, then remove the lock file return "$retval" # Get Return value else # Otherwise, do the following action "rsync stop fail" /bin/false # Rsync stop output fails to display false return $retval # Get the return value be else #otherwise echo "rsyncd process is not exist." Tip # rsync process does not exist return 2 # Returns the value 2 be else #otherwise echo "${rpfp}is not exist,or rsync does not startup" # Indicates the port number does not exist or rsync service did not start be #End } case "$1" in # Because writing a function, so we have here is more suitable case structure conditionals, variables defined as $ 1 start) # Match to start execution start and get the return value of the function start retval=$? ;; stop) # Match to stop the implementation of stop and get the return value of the function stop retval=$? ;; restart) # Match to restart executed first stop function sleep 1s achieved restart the execution start, and get return values stop sleep 1 start retval=$? ;; *) # No match to a prompt on the output can only perform three functions of echo " usage: $0 {start|stop|restart} " exit 1 esac #End exit $retval # Exit and thereby obtain the return value judgment
Test: smart you can see the PID process ID changes after our stop and restart open, indicating that the script ok.
[root@node1 ~]# lsof -i:873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 10044 root 4u IPv4 639767 0t0 TCP *:rsync (LISTEN) rsync 10044 root 5u IPv6 639768 0t0 TCP *:rsync (LISTEN) [root@node1 ~]# sh /server/scripts/09/rsyncd3.sh stop rsync stop ok [ OK ] [root@node1 ~]# lsof -i:873 [root@node1 ~]# [root@node1 ~]# sh /server/scripts/09/rsyncd3.sh start rsync startup ok [ OK ] [root@node1 ~]# lsof -i:873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 10212 root 4u IPv4 664268 0t0 TCP *:rsync (LISTEN) rsync 10212 root 5u IPv6 664269 0t0 TCP *:rsync (LISTEN) [root@node1 ~]# sh /server/scripts/09/rsyncd3.sh restart rsync stop ok [ OK ] rsync startup ok [ OK ] [root@node1 ~]# lsof -i:873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 10227 root 4u IPv4 664325 0t0 TCP *:rsync (LISTEN) rsync 10227 root 5u IPv6 664326 0t0 TCP *:rsync (LISTEN)
This script copy and paste to use, and requires /etc/rsyncd.conf configured pid lock file