Basically, I have a systemd path that notices when a change happens and then calls a script I wrote that moves a file to another folder based on its file extension.
The issue I am running into is that the file isn’t done downloading and it is moving. Systemd info is sometimes difficult to find. How can I make sure it is a complete file before it triggers the script?
Maybe explain a lot more, as said above, what you’re trying to do, but also how you are doing it. Even provide your scripts or whatever you use. Your thread is so vague I don’t think anyone can answer.
really? how is it vague? a systemd path monitors a folder and executes on change, that is something we can all agree on.
I can show you my script but its just a bunch of mv commands. Nothing you havent seen already, nothing that is important unless mv isnt the right command. I dont think you need to see the script, but if you want to, I can post it, but I am not confident it is important
I would think there is a way to make the path file wait until a file is complete before executing the script.
I want a script to execute when a file is added to my downloads folder, the script will move the files based on their extension to other folders. I am using something called a systemd path to monitor the folder. It detects a change in the folder and then executes a script.
EVERYTHING IS WORKING.
Except it is moving the files BEFORE they are finished downloading. This is the problem.
I’m sorry but I don’t know what else to say, if you can tell me what specific part confuses you I can elaborate. But you’re just confusing me with your confusion lol
You may do that from your script (as suggested by winnie). Just quick thought (no code example):
Make your script have a loop to list the content and size of files in the folder
If the size of file(s) is not changing after x amount of loops, then proceed to moving file(s)
Make your script check if it is currently looping or not (because your systemd thing may trigger all the time during a file download), and if not looping, then continue, if already looping, then exit. You could do that I guess by having the script create an empty file somewhere outside of that folder, at the start of the script loop (and have your script delete that empty file after it has finished moving the files). If the script finds that file when starting, then exit, if it does not find that file, then create it and continue.
Maybe you can find completely different approach than the systemd thing too.
I’m beginning to think systemd might not be the right solution as you are saying.
I did find a simple “solution” but I am not confident that it does what I think it is doing.
I changed: PathModified to PathChanged.
It correctly copied the file. But part of me thinks there is a catch to this, I will have to dig into the man pages on this one, a random article I found on brave search yielded this idea lol…
PathChanged= may be used to watch a file or directory and activate the configured unit whenever it changes. It is not activated on every write to the watched file but it is activated if the file which was open for writing gets closed. PathModified= is similar, but additionally it is activated also on simple writes to the watched file.
When you download a file the browser is creating a zero length file with the target name + a file with filename.ext.temp where temp is different for each browser.
So watching the folder - poll for temp name - and only when the temp name no longer exist the script can copy the target to its new location.
While the systemd watch PathChanged is interesting - it may be worth checking if the zero-byte target file is still open while the download is stored in temp file - I mean watching the wrong file and moving a zero-byte is not optimal and most likely what you want to avoid.