I am setting up my first cron on a Ubuntu Desktop system. I was curious to know if there is a way to lock a folder before a cron is ran and how to set it up? I am following this article. I am just trying to make sure I don't damage any files when moving them from and to the cron folder because it is on a shared network.
-
You are doing a mv? instead of using the cron command to import/export crontabs? Why?mdpc– mdpc2013-02-05 21:26:17 +00:00Commented Feb 5, 2013 at 21:26
-
Im not doing a move. I have setup a shared folder on a network that will allow me to drop files to that folder. I created some .sh script that works inside that folder, but I want to make sure I dont mess up any files if I move files (on the network) to the folder and the cron is running. I thought about creating an addition to the .sh script that will move files to another folder and put them back. Looking for ideas.DᴀʀᴛʜVᴀᴅᴇʀ– DᴀʀᴛʜVᴀᴅᴇʀ2013-02-05 21:35:51 +00:00Commented Feb 5, 2013 at 21:35
3 Answers
A simple script with locking and a delay before accessing files:
#!/bin/sh
if mkdir /tmp/myscript-running; then
cd /mnt/share/whereever
find . -type f -mmin +1 -print0 | xargs -i -n 1 -0 myscript.sh "{}"
rmdir /tmp/myscript-running
else
: # previous instance still running, do nothing
fi
GNU find or equivalent required for the -mmin option. You can run this as often as required via cron. Replace myscript.sh with your processing script.
The main features are:
- use
mkdirto create a lock directory to prevent overlapping instances - use
findwith-mmin +1which limits the output to files modified more than 1 minute ago, this is to try to ensure new files are fully copied - use
xargsto process the files one at a time,"{}"is replace by the filename - use
\0terminated filenames withfind | xargsso that troublesome filenames don't cause problems
You should be able to modify the find parameters to match what you need. You can also use the above logic to move completed files from an "uploading/" to a "ready/" directory, which might simplify things.
Ubuntu also has lockfile and shlock which can be useful, see the man page of the latter for more ideas.
This isn't ideal, but effective. You have a separate, 'feeder' directory that you copy new files into, when copying them onto the network. Your cron script can then perform an /ls of /var/feeder, then lsof each file in that directory looking for any open file handles. Files that aren't in use are then moved into the main shared directory, files that are open (presumably you're copying them or some such) are left in the feeder. Ensure the main /shared directory isn't writable except by your script, and you should be good.
step by step guide to lock folders in linux http://linux-all-over.blogspot.in/2014/10/how-to-lock-filefolder-in-linux-kali.html
-
1Please post the most important, relevant, and helpful information from your link in-line. Together, we can help avoid link-rot!HalosGhost– HalosGhost2014-11-04 04:21:04 +00:00Commented Nov 4, 2014 at 4:21
-
The link doesn't have anything to do with concurrent file/directory locking; it's about encrypting contents with truecrypt. Not helpful at allOblong2824– Oblong28242016-01-21 22:52:21 +00:00Commented Jan 21, 2016 at 22:52