[Solved] How do I check for a file having copied fully?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
Pastcal
Level 3
Level 3
Posts: 180
Joined: Tue May 07, 2013 12:06 pm

[Solved] How do I check for a file having copied fully?

Post by Pastcal »

A bit of background: I'm trying to turn a Linux Mint system (that I use as a desktop) into a print server. I've found that LM can print a pdf file from the command line ($ lp file_to_print.pdf). What I'm now trying to do is create a shell script to check a shared sub-directory for the presence of a file with a specific name, then print and delete that file. Why am I doing it this way? To avoid the problems I've had with the print subsystems (lpr/lpd and CUPS) on a FreeBSD server. This approach completely bypasses the FreeBSD print sub-sytems, which have given me so much grief.

What I've got so far:(a shared sub-directory called /autoprint and a shell script called autoprint.sh )

Code: Select all

#! /bin/bash
# autoprint.sh
# make print files with the name printfile.pdf
while :
do
lp /autoprint/printfile.pdf && rm /autoprint/printfile.pdf
sleep 5
done
The problem I foresee is that autoprint.sh doesn't check for the print file (printfile.pdf) being copied completely.

Any suggestions? Advice please. Tar, Pastcal
Last edited by Pastcal on Sun Jan 28, 2024 2:19 pm, edited 2 times in total.
Hawaiihemd
Level 4
Level 4
Posts: 409
Joined: Fri Sep 25, 2020 12:42 pm

Re: How do I check for a file having copied fully?

Post by Hawaiihemd »

I think the fuser command should help.
Pastcal
Level 3
Level 3
Posts: 180
Joined: Tue May 07, 2013 12:06 pm

Re: How do I check for a file having copied fully?

Post by Pastcal »

Thanks
Hawaiihemd wrote: Tue Jan 02, 2024 7:58 am I think the fuser command should help.
This-> https://en.wikipedia.org/wiki/Fuser_(Unix)
I'm not clear on how to use it. An example would be helpful. Thanks.
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: How do I check for a file having copied fully?

Post by Koentje »

,,
Last edited by Koentje on Sat Jan 06, 2024 5:07 pm, edited 1 time in total.
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: How do I check for a file having copied fully?

Post by Koentje »

,,
Last edited by Koentje on Sat Jan 06, 2024 5:07 pm, edited 1 time in total.
dave0808
Level 5
Level 5
Posts: 987
Joined: Sat May 16, 2015 1:02 pm

Re: How do I check for a file having copied fully?

Post by dave0808 »

Pastcal wrote: Tue Jan 02, 2024 6:13 am The problem I foresee is that autoprint.sh doesn't check for the print file (printfile.pdf) being copied completely.
Common practice is to do the upload/copy with a different name and then rename it once the copying is complete. You'll see browsers doing something similar when you download a large file.
Pastcal
Level 3
Level 3
Posts: 180
Joined: Tue May 07, 2013 12:06 pm

Re: How do I check for a file having copied fully?

Post by Pastcal »

dave0808 wrote: Tue Jan 02, 2024 12:39 pm
Pastcal wrote: Tue Jan 02, 2024 6:13 am The problem I foresee is that autoprint.sh doesn't check for the print file (printfile.pdf) being copied completely.
Common practice is to do the upload/copy with a different name and then rename it once the copying is complete. You'll see browsers doing something similar when you download a large file.
Yes, that seems a simple way forward. Still it seems like there's still the matter of determining when the copy has completed...Hmmm, I may have underestimated the difficulty of this. Gah, curse you FreeBSD printing sub-systems...
User avatar
Coggy
Level 5
Level 5
Posts: 642
Joined: Thu Mar 31, 2022 10:34 am

Re: How do I check for a file having copied fully?

Post by Coggy »

You can check that file a: exists, and b: nothing has the file open (e.g. for writing, use fuser to check that), and only then try to print it.
You may also want to wait until lp has finished reading the file before deleting it, maybe again using fuser to see if anything has it open.
Hawaiihemd
Level 4
Level 4
Posts: 409
Joined: Fri Sep 25, 2020 12:42 pm

Re: How do I check for a file having copied fully?

Post by Hawaiihemd »

I would then simply check the return code of fuser stored in the variable $? if fuser was the last command being executed.
If it is non-zero, nothing has the file(s) open.
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: How do I check for a file having copied fully?

Post by Shiva »

Pastcal wrote: Tue Jan 02, 2024 6:13 am The problem I foresee is that autoprint.sh doesn't check for the print file (printfile.pdf) being copied completely.
You can check the copy process from the start by using the wait command :

Code: Select all

cp "source" "dest" & wait -n
will run the cp command process and wait until this process is over

You can also check the exit code $? : if 0 everything went fine, if 1 something's gone wrong (path error, insufficient privilege ...)

If your copy process is really long (should not be the case with pdf files), you can use another terminal window to do something else. The one using wait is frozen till the job's over.
And of course the & wait -n command can be applied to any process you want to be sure it's finished especially in scripts when you absolutely need something has to be finished before some other begins.
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: How do I check for a file having copied fully?

Post by Koentje »

,,
Last edited by Koentje on Sat Jan 06, 2024 5:07 pm, edited 1 time in total.
Pastcal
Level 3
Level 3
Posts: 180
Joined: Tue May 07, 2013 12:06 pm

Re: How do I check for a file having copied fully?

Post by Pastcal »

Thanks all for useful replies. I may have to rethink this as it's becoming more complex that I anticipated.
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: How do I check for a file having copied fully?

Post by Koentje »

,,
Last edited by Koentje on Sat Jan 06, 2024 5:08 pm, edited 1 time in total.
dave0808
Level 5
Level 5
Posts: 987
Joined: Sat May 16, 2015 1:02 pm

Re: How do I check for a file having copied fully?

Post by dave0808 »

Pastcal wrote: Tue Jan 02, 2024 12:45 pm Yes, that seems a simple way forward. Still it seems like there's still the matter of determining when the copy has completed...
That's for the client to do...

Code: Select all

cp file_to_print.pdf /autoprint/printfile$$.part && mv /autoprint/printfile$$.part /autoprint/printfile.pdf
You'll want to check that "printfile.pdf" doesn't exist first otherwise you might overwrite someone else's print submission.

Or, change your server script to print any file with a ".pdf" suffix, or plenty of other ways of achieving the same result.
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: How do I check for a file having copied fully?

Post by Koentje »

,,
Last edited by Koentje on Sat Jan 06, 2024 5:08 pm, edited 1 time in total.
dave0808
Level 5
Level 5
Posts: 987
Joined: Sat May 16, 2015 1:02 pm

Re: How do I check for a file having copied fully?

Post by dave0808 »

It can be useful though won't detect the case that the file isn't open because the connection failed midway through the upload or the copy was aborted.

If the client is responsible for renaming upon successful completion, then there are no concerns about dealing with part files.
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: How do I check for a file having copied fully?

Post by Koentje »

Never mind..
deepakdeshp
Level 20
Level 20
Posts: 12341
Joined: Sun Aug 09, 2015 10:00 am

Re: How do I check for a file having copied fully?

Post by deepakdeshp »

If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
Pastcal
Level 3
Level 3
Posts: 180
Joined: Tue May 07, 2013 12:06 pm

Re: [Solved] How do I check for a file having copied fully?

Post by Pastcal »

I went a different route but got a notionally simpler solution;
I used Samba installed on the FreeBSD system, sharing a folder[1] with the sticky bit removed[2].

I created this shell script:

Code: Select all

# autoprint.sh
# automatically prints files with the name
# autoprint.pdf in /tmp on 192.168.0.5
# then deletes the file


# mount /tmp on 192.168.0.5
gio mount --anonymous smb://192.168.0.5/tmp
# files are mounted here -> /run/user/1000/gvfs/smb-share:server=192.168.0.5,share=tmp/

# wait for mount to take place
sleep 5

# check mount for autoprint.pdf, if found print and delete
while :
do
lp -dHL-L2300D /run/user/1000/gvfs/smb-share:server=192.168.0.5,share=tmp/autoprint.pdf && echo printing autoprint.pdf && rm /run/user/1000/gvfs/smb-share:server=192.168.0.5,share=tmp/autoprint.pdf
sleep 5
done
Run the shell script from
cron
or a desktop shortcut/launcher. Print files to a pdf file, save into /tmp with the default name, then rename to autoprint.pdf.

[1] I use /tmp as seen in the shell script. This is what my smb4.conf looks like:

Code: Select all

# /usr/local/etc/smb4.conf 12 sept 2022 MG #

[global]
        server string = PR5 Samba Server
#        security = share
        guest account = sambobulous
        create mask = 0777
        inherit permissions = yes
[server_share]
        path = /home/sambobulous/server_share
        writable = yes
        guest ok = yes
        browseable = yes
        create mask = 0777
[tmp]
        path = /tmp
        writeable  = yes
        guest ok = yes
        browseable = yes
        create mask = 0777
[2] man chmod(1) - see the 't' option
[3] I use Linux Mint Xfce. The printer is a Brother HL-L2300D.
Post Reply

Return to “Scripts & Bash”