0

I need to sync a directory between two servers and decided to create a service which listens to changes in a file and runs rsync.

/usr/lib/systemd/system/mediasync.service

[Unit]
Description=SyncCdnImages

[Service]
#Type=simple
ExecStart=/usr/bin/mediasync.sh
#Restart=on-abort

[Install]
WantedBy=multi-user.target   

/usr/bin/mediasync.sh

#!/bin/sh
inotifywait -mr -e close_write -e create "/var/www/html/folder/backend/utils/sync.log" | while read line

do
        rsync -avzr --update /var/www/html/cdnimages/* myip:/var/www/html/cdnimages/cdnimages/
done

ps aux | grep sync

root     25540  0.0  0.0 115244  1424 ?        Ss   11:31   0:00 /bin/sh /usr/bin/mediasync.sh
root     25541  0.0  0.0   6472   408 ?        S    11:31   0:00 inotifywait -mr -e close_write -e create /var/www/html/folder/backend/utils/sync.log
root     25542  0.0  0.0 115244   392 ?        S    11:31   0:00 /bin/sh /usr/bin/mediasync.sh

I start the service using systemctl start mediasync but what I don't understand is why it appears twice in the process list. Is this normal behavior?

Edit:

Killing one of the services for ex kill 25540, kills the other two as well.

Ciprian
  • 107
  • 4

1 Answers1

1

The while in your bash script is executed as a forked child process of the original bash.

This is normal behaviour.

If you do ps auxf you'll see the parent/child relationship, although it wont say while as the command name as while is a bash internal command.

Matthew Ife
  • 23,357
  • 3
  • 55
  • 72