I have this script below that will be daemonized and triggered possibly hundreds if not thousands of times by different users.
The script uses inotifywait to watch a folder for an upload and then move the uploaded file to its final destination for presentation, after rotating (backup/move) previous uploads. The code will be run against different upload folders that are created on the fly.
#!/bin/bash
db="$VAR1"/path/to/upload/folder/
s3="$VAR2"/path/to/remote/folder
inotifywait -m -r -e attrib "$db" |
while read dir ev file;
do
for dirnum in $(cd "$s3"; ls */*.png | sed 's%/.*%%' | sort -nr)
do
next=$(($dirnum + 1));
mv "$s3/$dirnum/post$dirnum.png" "$s3/$next/post$next.png";
done
mv "$db"/"$file" "$s3"/1/post1.png
done
What can I do to optimize it? Or should it be rewritten a faster programming language? Also, how can I test scripts under a certain amount of load?
This does not give identical behavior, but it avoids sorting:
If you keep track of the highest number stored to
$s3, you can avoid the firstloop. Doing so is slightly more fragile if other processes are creating files
in
$s3, but in that case you have a race condition even in this simplistic solution.It would be a lot simpler to not rename the files, but put the first file uploaded in
$s3/1, and the next in$s3/2. In that case the script can be written: