Systemd path in Downloads folder. Can it be made to wait until the download is finished before activating the script?

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?

Perhaps it would be beneficial
if you explained
what it is
that you want to do …

Maybe something in the script that polls the file every second and doesn’t move the file until the filesize remains constant for more than a second? :person_shrugging:

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.

I have a path file that is monitoring a folder. It detects a change and then executes a script

Everything I just wrote in the above sentence is already done. The problem has not been mentioned yet.

problem: it executes the script even if the file is still downloading. I don’t want to do this. I want the file to be done before it moves.

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.

[Unit]
Description = Moves files to good folders.

[Path]
PathModified=/home/me/Downloads
Unit=dl-destroyer.service

[Install]
WantedBy=timers.target

That sounds difficult lol. I don’t know how to do that

it just is

is still very much valid

But I will not insist anymore …
but just let it
and your issue
go :wink:

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

… of course - if you have the script monitoring for a … change
… instead of a completed downloaded file …

How is “the script” going to determine whether the download is finished?

So, do you have any ideas on how I can make it wait for it to finish? This is what I am asking

no - someone may
I do not.

edit:

Browsers may/can do that
they are calling the downloaded file
xxx.abc.part
and rename it (to lose the .part extention)
when the download is complete

I do not know how they are monitoring the progress.
But they do.

/edit

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…

Path does not change - just the file size does (during download) …

… Browsers do it
see above -
how they do it:
I don’t know

… and this still lacks information …

I’m out :wink:

From a quick Google

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.

1 Like

There’s two different things going on:

  • Systemd “Path” is agnostic to and unaware of…
  • …the application (e.g, web browser) which is downloading files that remain in a “partial” state until they complete.

If PathChanged magically solves this, then all the better, though I’m not sure how well it will consistently work for your unique usage.

Interesting thread that might be of interest.

https://serverfault.com/questions/415596/determine-if-file-is-in-the-process-of-being-written-upon

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.

Nobody said it was a download from a web browser ( ͡° ͜ʖ ͡°) and as we don’t need to know more, I assume it is “not important” to mention that.

1 Like