[英]How can I lock a file from a program started as a cron job on Linux?
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 我在代码中使用fcntl来锁定文件并解锁以在Windows中像互斥锁一样练习...我在Linux中手动启动我的应用程序,结果正确,应用程序运行平稳...但是我被要求制作一个bash脚本每天启动应用程序。...我的脚本是
cd myapppaht
./myapp
however, i got [Bad file descriptor]
when it try to lock a file position... is the crontab task practice not powerful as manual user root? 但是,当我尝试锁定文件位置时出现[Bad file descriptor]
... crontab任务练习作为手动用户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. 在没有看到您的锁定代码或不知道如何从cron启动的情况下,没有太多事情要做。
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. 根据有关文件锁的GNU C库手册,当请求未打开文件描述符进行写访问的写锁时,您可以获得EBADF
(错误文件描述符)。
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. fcntl手册页补充说,当使用F_SETLKW
,如果文件描述符打开模式与请求的锁定类型不匹配,则可以获取EBADF
。 Since your app runs smoothly when started manually, I doubt this is the issue. 由于您的应用程序在手动启动时运行平稳,因此我怀疑这是问题所在。
open
. 由此,我将检查您对open
的调用的响应。 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. 根据此Linux crontab手册页 ,每个用户都有自己的crontab,并且任何给定crontab中的命令都将以拥有crontab的用户身份执行。 This is dependent on the version of cron as some (older) versions specify the user in the crontab file itself. 这取决于cron的版本,因为某些(旧)版本在crontab文件本身中指定了用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.