We’re rewarding the question askers & reputations are being recalculated! Read more.

Hot answers tagged

108

sometimes it's the result of mounting issues, so I'd unmount the filesystem or directory you're trying to remove: umount /path


45

Here's another way to do locking in shell script that can prevent the race condition you describe above, where two jobs may both pass line 3. The noclobber option will work in ksh and bash. Don't use set noclobber because you shouldn't be scripting in csh/tcsh. ;) lockfile=/var/tmp/mylock if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null;...


42

lslocks, from the util-linux package, does exactly this. In the MODE column, processes waiting for a lock will be marked with a *.


27

Two possibilities: lsof (my preference) or lslk (specifically for file locks): [root@policyServer ~]# lslk | grep "master.lock" SRC PID DEV INUM SZ TY M ST WH END LEN NAME master 1650 253,0 12423 33 w 0 0 0 0 0 /var/lib/postfix/master.lock [root@policyServer ~]# lsof | grep "master.lock" master 1650 root 10uW ...


23

Unix systems by and large avoid mandatory locks. There are a few cases where the kernel will lock a file against modifications by user programs, but not if it's merely being written by another program. No unix system will lock a file because a program is writing to it. If you want concurrent instances of your script not to tread on each others' toes, you ...


22

Why does the script need to redirect, to a file descriptor inherited by the subshell, a copy of its own contents rather than, say, the contents of some other file? You could use any file, as long as all copies of the script use the same one. Using $0 just ties the lock to the script itself: If you copy the script and modify it for some other use, you don't ...


18

Since you're using >>, which means append, each line of output from each instance will be appended in the order it occurred. If your script output prints 1\n through 5\n with a one second delay between each and instance two is started 2.5 seconds later you'll get this: 1 2 1 3 2 4 3 5 4 5 So to answer your question: No.


18

Almost like nsg's answer: use a lock directory. Directory creation is atomic under linux and unix and *BSD and a lot of other OSes. if mkdir $LOCKDIR then # Do important, exclusive stuff if rmdir $LOCKDIR then echo "Victory is mine" else echo "Could not remove lock dir" >&2 fi else # Handle error condition ....


18

To add to Bruce Ediger's answer, and inspired by this answer, you should also add more smarts to the cleanup to guard against script termination: #Remove the lock directory function cleanup { if rmdir $LOCKDIR; then echo "Finished" else echo "Failed to remove lock directory '$LOCKDIR'" exit 1 fi } if mkdir $LOCKDIR; then ...


17

This problem has been reported since 2009 (google for 'gedit virtualbox'). It is awful that there's no fix for it yet. Neither VirtualBox nor Gedit developers are willing to take responsibility for it, and instead are content to point fingers at one another for over three years. You can set your editor preferences to 'Create a backup' then save twice. ...


17

You might want to take a look at fail2ban. It can be configured to lock an account after a set number of failed attempts, and then unlock after a set period of time. http://www.fail2ban.org/wiki/index.php/Downloads If you're really serious about using pam_tally, you probably want to use pam_tally2 instead. Should be installed with any PAM package that's ...


15

File locks aren't mandatory1 -- i.e., you can't lock a file so that another process cannot access it. Locking a file means that if a(nother) process checks to see if it has been locked, it will know. The purpose of flock is to do stuff like what you want, but you must then use flock for each and every attempted access. Keep in mind those are blocking ...


15

This is not a dpkg-specific issue (as the title of my edit suggested). Rather, this is something that every package manager (of which I am aware) does; and for good reason. Though, I do understand why it might be confusing. Package managers rely on databases to track the information for installed packages. If multiple users attempt to write to a database at ...


14

I prefer to use hard links. lockfile=/var/lock/mylock tmpfile=${lockfile}.$$ echo $$ > $tmpfile if ln $tmpfile $lockfile 2>&-; then echo locked else echo locked by $(<$lockfile) rm $tmpfile exit fi trap "rm ${tmpfile} ${lockfile}" 0 1 2 3 15 # do what you need to Hard links are atomic over NFS and for the most part, mkdir is as ...


12

Here is the solution: Go into the directory and type ls -a You will find a .xyz file vi .xyz and look into what is the content of the file ps -ef | grep username You will see the .xyz content in the 8th column (last row) kill -9 job_ids - where job_ids is the value of the 2nd column of corresponding error caused content in the 8th column Now try to delete ...


12

I understand that mkdir is atomic, so perhaps: lockdir=/var/tmp/myapp if mkdir $lockdir; then # this is a new instance, store the pid echo $$ > $lockdir/PID else echo Job is already running, pid $(<$lockdir/PID) >&2 exit 6 fi # then set traps to cleanup upon script termination # ref http://www.shelldorado.com/goodcoding/tempfiles.html ...


12

I found these methods on Ubuntu Forums in a thread titled: Thread: How do I lock the screen in XFCE?. excerpted from 2 of the answers in that thread Method #1 - Keyboard shortcut Open the settings manager > keyboard > shortcuts and you can see that the default shortcut to lock the screen is ctrl-alt-del. If you want to change it, click add on the left, ...


12

For most use cases of flock, it's very important that the lock file not be "cleaned up". Otherwise, imagine this scenario: process A opens the lock file, finds it does not exists, so it creates it. process A acquires the lock process B opens the lock (finds it already exists) process B tries to acquire the lock but has to wait process A releases the lock ...


10

If you don't need to modify the file then the chattr +i will lock the file from deletion. This will mark the file immutable. Otherwise file deletion is controlled by the directory containing the file. Anyone with write access to the directory can delete most of the files it contains. Files with the sticky bit set can only be deleted by the owner. Files ...


10

Bash's processing of the command below may be surprising: flock -x -w 5 /dev/shm/counter.txt echo "4" > /dev/shm/counter.txt && sleep 5 Bash first runs flock -x -w 5 /dev/shm/counter.txt echo "4" > /dev/shm/counter.txt and, if that completes successfully (releasing the lock), then it runs sleep 5. Thus, the lock is not held for the 5 ...


9

I had this same issue, built a one-liner starting with @camh recommendation: lsof +D ./ | awk '{print $2}' | tail -n +2 | xargs kill -9 The awk command grabs the PIDS. The tail command gets rid of the pesky first entry: "PID". I used -9 on kill, others might have safer options.


9

The lock file is used to prevent parallel execution of multiple instances. Why is this important for a package managers? A package manager — from a high level view — is a program which applies complex changes to the hard disk. The changes cannot be done in one step (“atomic”), so there are multiple steps; many of the steps depend on the result of ...


9

A file lock is attached to a file, through a file description. At a high level, the sequence of operations in one instance of the script is: Open the file to which the lock is attached (“the lock file”). Take a lock on the lock file. Do stuff. Close the lock file. This releases the lock that is attached to the file description created by opening a file. ...


8

If you only want one instance of your app running you can use a lock file. Open it with O_CREAT|O_EXCL flags and it will fail if the file already exists. If you want to synchronize access to a file use flock. It is also possible to lock parts of files with fcntl. Flock is only for advisory locking meaning a program can ignore the locks and access it anyway. ...


8

An easy way is to use lockfile coming usually with the procmail package. LOCKFILE="/tmp/mylockfile.lock" # try once to get the lock else exit lockfile -r 0 "$LOCKFILE" || exit 0 # here the actual job rm -f "$LOCKFILE"


8

Advisory locking is for processes that cooperate "peacefully". The kernel keeps track of the locks but doesn't enforce them - it's up to the applications to obey them. This way the kernel doesn't need to deal with situations like dead-locks. Mandatory locking was introduced in System V Unix, but it turns out that the design was not the brightest thing. (...


8

Linux normally doesn't do any locking (contrary to windows). This has many advantages, but if you must lock a file, you have several options. I suggest flock: apply or remove an advisory lock on an open file. This utility manages flock(2) locks from within shell scripts or from the command line. For a single command (or entire script), you can use ...


8

These files are NFS placeholders: /home/johndoe/qwerty/.nfs000000000471494300000944 Some background In a typical UNIX filesystem, a file that is currently in use and open can be deleted but its contents will not actually disappear until the last filehandle to it is closed. You can see this in action with code like this: $ ps -ef >/tmp/temporaryfile $ ...


7

Invoke a shell explicitly. flock -x -w 5 ~/counter.txt sh -c 'COUNTER=$(cat counter.txt); echo $((COUNTER + 1)) > ~/counter.txt' Note that any variable that you change is local to that shell instance. For example, the COUNTER variable will not be updated in the calling script: you'll have to read it back from the file (but it may have changed in the ...


7

On Linux, the file is not locked even when a program is writing to it (unlike windows). To check if the process is completed, use: while [[ 1 ]]; do pgrep dbprocess &>/dev/null if [[ $? -eq 0 ]]; then echo still running else echo finished # start ftp transfer fi sleep 2 done To check if the process has the file open, you can ...


Only top voted, non community-wiki answers of a minimum length are eligible