开发者

How can I lock a file from a program started as a cron job on Linux?

开发者 https://www.devze.com 2022-12-19 16:41 出处:网络
I use fcntl in my codes to lock file and unlock to practice like mutex in windows... I start my app in linux manually, i got right result, with the app runs smoothly...

I use fcntl in my codes to lock file and unlock to practice like mutex in windows... I start my app in linux manually, i got right result, with the app runs smoothly... but i was asked to make a bash script to start the app daily.... my script is

cd myapppaht
./myapp

however, i got [Bad file descriptor] when it try to lock a file position... is the crontab task practice not po开发者_如何学JAVAwerful as manual user root?

#define writew_lock(fd , offset , whence , len)  lock_reg((fd) , F_SETLKW , F_WRLCK , (offset) , (whence) , (len))

#define un_lock(fd , offset , whence , len)  lock_reg((fd) , F_SETLK , F_UNLCK , (offset) , (whence) , (len))


Without seeing your locking code or knowing how it is being launched from cron, there isn't much to go on.

fcntl

According to the GNU C Library manual on File Locks, you can get EBADF (Bad file descriptor) when requesting a write lock where the file descriptor is not open for write access.

The fcntl man page adds that you can get EBADF when using F_SETLKW where the file descriptor open mode doesn't match with the type of lock requested. Since your app runs smoothly when started manually, I doubt this is the issue.

  • From this, I would check the response of your call to open.

crontab

According to this Linux crontab man page, each user has their own crontab, and commands in any given crontab will be executed as the user who owns the crontab. This is dependent on the version of cron as some (older) versions specify the user in the crontab file itself.

  • You might also check into your crontab setup.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号