I have a server that receives
backupname.tar.gz
files in/home/my_user/drop
directory every hour.I installed incron utility and use incrontab -e entry to run a script whenever a new file shows up in /drop
Here is the script:
#!/bin/sh
#
# First clear the 2 immediate use directories
rm /home/my_user/local_ready/*
wait
sleep 1
rm /home/my_user/local_restore/*
wait
sleep1
# Copy the file from the /drop /local_ready
cp /home/my_user/drop/*.tar.gz /home/my_user/local_ready/
wait
sleep 5
# Now move the file to the /current folder
mv /home/my_user/drop/*.tar.gz /home/my_user/current/
wait
sleep 1
# Next we delete any stray files dropped that are not
# of the target type so we can keep /drop clean.
rm /home/my_user/drop/*
wait
sleep 1
# Un-Tar the files into the /local_restore directory
tar -xzf /home/my_user/local_ready/*.tar.gz -C /home/my_user/local_restore/
wait
sleep 1
# This should complete the movement of files
The problem I have been running into is the file that gets copied to the /local_restore
directory is truncated as if the next command in the script is causing an interruption to the cp
command.
At first I put sleep commands in it to try to get it to work, then I added wait commands after each command in the script to try to get it to work thinking that would force everything to wait until the cp command had finished copying the file to the next location.
I cannot even tell if the tar
command is working at all because it depends on the success of the cp
command further up the chain to have the file in place. Based on a test I ran with only a command to un-tar one of the files, I suspect it will not complete either before the script exits either. At least that occurred in a different 3 line test I used to test my timing theory.
BTW... the mv
command works just fine and the whole file gets moved as it should.
Can anyone identify why the commands run in the script seem to be unable to complete their task?
I have been asked to show the contents of the incrontab entry so here it is:
/home/my_user/drop/ IN_CREATE /home/my_user/bin/cycle_backups
(cycle_backups is obviously the name of the script file)
This is a KVM type VPS cloud server running Ubuntu 16.04 LTS and it has 10gb of memory with over 100gb of disk space. When the file is dropped, this is the only thing the server has to do other than system idle!
I will admit that my server is a bit slow, so when trying to copy a 200mb file to another directory it takes a second or two to complete even when I do it right at the command line.
I am at a loss to explain the problem, which makes it even harder to identify a solution.
Fair Warning: I am not the best at any of this, but I didn't think this should be such an impossible thing to accomplish.
wait
is pointless and unnecessary. Commands run synchronously in a script unless you explicitly state otherwise. – Chris Davies Apr 09 '19 at 22:53wait
is used to wait for background processes to finish before continuing. There's not a single background process in your script, so thewait
s are pointless, as are all thesleep
s. And you know that, since the problem existed before you added all thosewait
s andsleep
s. – muru Apr 10 '19 at 01:43incrontab -l
so we can see how your script is triggered. – Freddy Apr 10 '19 at 02:08IN_CREATE
toIN_CLOSE_WRITE
as alecxs already pointed out. This should trigger the script when the tar.gz was fully written (and not when it was created while being uploaded). – Freddy Apr 10 '19 at 15:18