简体   繁体   English

如何在Linux上作为cron作业启动的程序中锁定文件?

[英]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启动的情况下,没有太多事情要做。

fcntl 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. 根据有关文件锁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. 由于您的应用程序在手动启动时运行平稳,因此我怀疑这是问题所在。

  • From this, I would check the response of your call to open . 由此,我将检查您对open的调用的响应。

crontab 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. 根据此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文件本身中指定了用户。

  • You might also check into your crontab setup. 您也可以检查crontab设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linux:如何找到持有特定锁的线程? - Linux: How can I find the thread which holds a particular lock? (Unix / Linux)如何从需要输入文件的另一个C程序执行C程序? - (Unix/Linux) How would I execute a C program from another C program that requires an input file? 在Linux上从桌面启动的程序的默认stdin和stdout是什么? - What are the default stdin and stdout for a program started from desktop on Linux? 如何从文件中读取数据以进行编程 - How can I read data from a file to program 如何在其默认程序中打开文件 - Linux - How do I open a file in its default program - Linux 如何在Linux平台上编译和运行ESQL / C程序? - How can I compile & and run an ESQL/C program on Linux Platform? 如何从AC程序准确地在Linux上复制文件 - How to copy a file on linux from a c program exactly 如何在Linux上通过inode访问文件 - How can I access file by inode on Linux 如何通过cron作业调度程序调用C程序上的函数以在ubuntu中每天调用该函数? - How to call a function on a C program by a cron job scheduler to call the function everyday in ubuntu? 当作为后台作业启动时,像 bc 这样的 Unix 程序如何干净地处理输入/输出流? - How does a Unix program like bc cleanly handle input/ouput stream when started as a background job?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM