Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running emacs from the command line and handling locked files

I'm using org-mode and am looking to export my agenda (example of an agenda) to a text file so that I can display it with conky. The org-mode manual features this command line example to do this:

emacs -batch -l ~/.emacs -eval '(org-batch-agenda "t")' | lpr

I've modified this like so:

emacs -batch -l ~/.emacs -eval '(org-batch-agenda "e")' \
> ~/org/aux/agenda-export.txt

I have this set as a cron job to run every 5 minutes. It works great unless I actually have emacs open. Then I noticed that the file agenda-export.txt was empty. In running this manually from the coammand line vs. through cron, I get this error (or similar depending on the files I have open):

...~/org/file.org locked by jwhendy (pid 10935): (s, q, p, ?)?

I was going to write a script to perhaps export to agenda-export-test.txt, then perhaps check for an empty file or no lines (wc -l == 0 maybe?). If true, leave the existing file alone and delete agenda-export-test.txt. If not, then move agenda-export-test.txt to agenda-export.txt.

But... when I try to run such a script, I'm met with the same emacs inquiry about whether to steal the lock, proceed, or quit. I'm fine with proceeding, as I don't think org-agenda does anything to the files and thus it wouldn't harm anything... but I don't know how to tell emacs to "force" or choose "proceed" if problems are encountered. I need something non-interactive.

So, to summarize, my thoughts were to try:

  • passing a --force option or similar to emacs to make it proceed at the pause
  • see if the exported tmp file has any lines and deal with it accordingly
  • tell emacs to run in "read only mode" (but I don't think it exists)

The main issue is that with cron, I'm not there to tell the process what to do, and so it just just make an empty file as the exported results. How can I handle this locked file business with a "blind" process like cron that can't respond?

I've tried asking the mailing list as well without a working outcome. [1] I wondered if someone here might have ideas.

[1] http://www.mail-archive.com/[email protected]/msg45056.html

like image 557
Hendy Avatar asked Dec 14 '25 02:12

Hendy


1 Answers

Have you tried copying file without using emacs? put in your crontab:

cp ~/org/file.org /tmp/export.org && emacs -batch .... /tmp/export.org ..

A regular "cp" command should not copy emacs locks. Of course, once in a while you might get a damaged file if you save agenda just during cp command, but this should not be too bad.

like image 122
theamk Avatar answered Dec 15 '25 21:12

theamk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!