Bash script + rsync: bash will not sync with the host?

I just wrote the actual scripts .shfrom the day this morning, and I got a little stuck. I am trying to write a script to check if a process is running and run it if it is not. (I plan to run this script every 10-15 minutes with cron.)

Here is what I still have:

#!/bin/bash

APPCHK=$(ps aux | grep -c "/usr/bin/rsync -rvz -e ssh /home/e-smith/files/ibays/drive-i/files/Warehouse\ Pics/organized_pics  imgserv@192.168.0.140:~/webapps/pavlick_container/public/images
")

RUNSYNC=$(rsync -rvz -e ssh /home/e-smith/files/ibays/drive-i/files/Warehouse\ Pics/organized_pics  imgserv@192.168.0.140:~/webapps/pavlick_container/public/images)

if [ $APPCHK < '2' ];
  then
    $RUNSYNC
fi

exit

Here is the error I get:

$ ./image_sync.sh 
rsync: mkdir "/home/i/webapps/pavlick_container/public/images" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(595) [Receiver=3.0.7]
rsync: connection unexpectedly closed (9 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(601) [sender=3.0.7]
./image_sync.sh: line 8: 2: No such file or directory

TRTWF is that

rsync -rvz -e ssh /home/e-smith/files/ibays/drive-i/files/Warehouse\ Pics/organized_pics imgserv@192.168.0.140:~/webapps/pavlick_container/public/images

works fine in the terminal window.

What am I doing wrong?

+3
source share
5 answers
  • Your call is grepincorrect for two counters. The template must not contain a new line. To search for the exact string, use grep -F 'substring'or grep -xF 'exact whole line'.
  • , ps | grep, . ( , Solaris, Linux * BSD) pgrep: pgrep -f 'PATTERN' true, , line PATTERN.
    • , 0, , 1 255, . ; 0 , - .
  • $(…) . , rsync , RUNSYNC. , ( , , ).
  • [ $APPCHK < 2 ] [ $APPCHK -lt 2 ]: < . ( bash [[ foo < bar ]], , .)
  • ~/ rsync . , -e ssh , rsync .
  • exit script , script .

a script, :

#!/bin/bash
 run_rsync () {
     rsync -rvz '/home/e-smith/files/ibays/drive-i/files/Warehouse Pics/organized_pics' \
           imgserv@192.168.0.140:webapps/pavlick_container/public/images
}
process_pattern='/usr/bin/rsync -rvz /home/e-smith/files/ibays/drive-i/files/Warehouse Pics/organized_pics imgserv@192\.168\.0\.140:webapps/pavlick_container/public/images'
if pgrep -xF "$process_pattern"; then
  run_rsync
fi
+5

, rsync, : ~/webapps/pavlick_container/public/images

192.168.0.140 imgserv, , "pavlick_container/public"? .

+1

. , . .

RUNSYNC="rsync -rvz -e ssh /home/e-smith/files/ibays/drive-i/files/Warehouse\ Pics/organized_pics  imgserv@192.168.0.140:~/webapps/pavlick_container/public/images"
if ! pgrep -f "rsync.*organized_pics"; then $RUNSYNC; fi
+1

, . . - , script, script. , script, , .

\ ~, ~/. cron , . , , , , , cron. , ~/ , .

0

dlb- grep, get-go.

, ps aux , ( , ps, ).

ps auxwww. , | grep -v grep | ( , - ). , "/usr/bin/rsync", "/usr/bin/[r] sync".

Other users also help with their comments. Using the flag file as references to @DiegoSevilla is slightly outdated. use mkdir /tmp/MyWatcher_flagDirfor your flag. Creating directories is atomic activity (where there is no file creation), and this will eliminate any errors that may occur when there are two copies of your monitor in order to try to create a flag file at the same time. Only one process will be able to create or remove the dir flag.

Hope this helps.

0
source

All Articles