Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
You are doing a mv? instead of using the cron command to import/export crontabs? Why? –  mdpc Feb 5 '13 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. –  Matt Feb 5 '13 at 21:35

3 Answers 3

up vote 1 down vote accepted

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:

  1. use mkdir to create a lock directory to prevent overlapping instances
  2. use find with -mmin +1 which limits the output to files modified more than 1 minute ago, this is to try to ensure new files are fully copied
  3. use xargs to process the files one at a time, "{}" is replace by the filename
  4. use \0 terminated filenames with find | xargs so 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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
1  
Please post the most important, relevant, and helpful information from your link in-line. Together, we can help avoid link-rot! –  HalosGhost Nov 4 at 4:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.