From mboxrd@z Thu Jan 1 00:00:00 1970 From: Giri Prashanth Subject: Re: [0] Run org-babel src block periodically Date: Thu, 7 Jan 2016 23:44:13 -0800 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a11421ec4f0fce10528cdc0e2 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:33562) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aHRia-00088L-MN for emacs-orgmode@gnu.org; Fri, 08 Jan 2016 02:44:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aHRiZ-00056j-3V for emacs-orgmode@gnu.org; Fri, 08 Jan 2016 02:44:16 -0500 Received: from mail-yk0-x233.google.com ([2607:f8b0:4002:c07::233]:33958) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aHRiY-00056d-Tl for emacs-orgmode@gnu.org; Fri, 08 Jan 2016 02:44:15 -0500 Received: by mail-yk0-x233.google.com with SMTP id a85so276514893ykb.1 for ; Thu, 07 Jan 2016 23:44:14 -0800 (PST) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Cc: emacs-orgmode --001a11421ec4f0fce10528cdc0e2 Content-Type: text/plain; charset=UTF-8 Thank you. This works great. -GP On Sun, Jan 3, 2016 at 2:30 PM, John Kitchin wrote: > This is kind of close to what you want I think (at least it updates > every 5 seconds for me, 5 times before it stops). You might adapt it to > do what you want. > > This is the block to run periodically. > #+name: time-string > #+BEGIN_SRC sh > date > top -o "cpu" -ncols 4 -l 1| head -n 15 > #+END_SRC > > #+RESULTS: time-string > #+begin_example > Sun Jan 3 17:28:27 EST 2016 > Processes: 225 total, 2 running, 3 stuck, 220 sleeping, 1110 threads > 2016/01/03 17:28:27 > Load Avg: 1.20, 1.51, 1.55 > CPU usage: 5.0% user, 22.50% sys, 72.50% idle > SharedLibs: 14M resident, 12M data, 0B linkedit. > MemRegions: 57263 total, 3092M resident, 72M private, 450M shared. > PhysMem: 6508M used (1098M wired), 1246M unused. > VM: 588G vsize, 1067M framework vsize, 6471076(0) swapins, 6831574(0) > swapouts. > Networks: packets: 190809878/201G in, 277243826/231G out. > Disks: 11873268/180G read, 10075469/265G written. > > PID COMMAND %CPU TIME > 96378 com.apple.InputM 0.0 00:00.26 > 96376 AppleSpell 0.0 00:00.68 > 96015 head 0.0 00:00.00 > #+end_example > > This function will update that block. We increment a counter and cancel > the timer when it is exceeded. > #+BEGIN_SRC emacs-lisp > (defun update-src-block (fname src-name N) > "Update the src-block named SRC-NAME in the file FNAME upto N times." > (if (> *counter* N) > (progn (cancel-timer *timer*) > (message-box "Done with timer")) > (save-excursion > (with-current-buffer (find-file-noselect fname) > (org-babel-goto-named-src-block src-name) > (org-babel-execute-src-block) > (incf *counter*))))) > #+END_SRC > > #+RESULTS: > : update-src-block > > We use a global var for the counter and timer. > #+BEGIN_SRC emacs-lisp > (setq *counter* 0) > (setq *timer* (run-with-timer > nil 5 > 'update-src-block > "/Users/jkitchin/ > blogofile-jkitchin.github.com/_blog/blog.org" > "time-string" > 5)) > #+END_SRC > > #+RESULTS: > : [nil 22153 41074 708648 5 update-src-block ("/Users/jkitchin/ > blogofile-jkitchin.github.com/_blog/blog.org" "time-string" 5) nil 0] > > > Giri Prashanth writes: > > > Hi, > > > > I would like to run a org-src block code periodically, is there any > > property for org-blocks for such repeated execution. > > > > For example if I want to watch the tail of a file periodically every 1 > min > > for next 1 hour, I would like to set a property like repeat for times to > > repeat and keep the org file auto updated > > > > #+BEGIN_SRC shell :repeat 60 :repeat-every 60sec > > tail -n 10 file > > #+END_SRC > > > > Thank you, > > -GP > > -- > Professor John Kitchin > Doherty Hall A207F > Department of Chemical Engineering > Carnegie Mellon University > Pittsburgh, PA 15213 > 412-268-7803 > @johnkitchin > http://kitchingroup.cheme.cmu.edu > -- -giri --001a11421ec4f0fce10528cdc0e2 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Thank you. This works great.

-GP
<= /div>

On Sun, Jan = 3, 2016 at 2:30 PM, John Kitchin <jkitchin@andrew.cmu.edu> wrote:
This is kind of close to what= you want I think (at least it updates
every 5 seconds for me, 5 times before it stops). You might adapt it to
do what you want.

This is the block to run periodically.
#+name: time-string
#+BEGIN_SRC sh
date
top -o "cpu" -ncols 4 -l 1| head -n 15
#+END_SRC

#+RESULTS: time-string
#+begin_example
Sun Jan=C2=A0 3 17:28:27 EST 2016
Processes: 225 total, 2 running, 3 stuck, 220 sleeping, 1110 threads
2016/01/03 17:28:27
Load Avg: 1.20, 1.51, 1.55
CPU usage: 5.0% user, 22.50% sys, 72.50% idle
SharedLibs: 14M resident, 12M data, 0B linkedit.
MemRegions: 57263 total, 3092M resident, 72M private, 450M shared.
PhysMem: 6508M used (1098M wired), 1246M unused.
VM: 588G vsize, 1067M framework vsize, 6471076(0) swapins, 6831574(0) swapo= uts.
Networks: packets: 190809878/201G in, 277243826/231G out.
Disks: 11873268/180G read, 10075469/265G written.

PID=C2=A0 =C2=A0 COMMAND=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 %CPU TIME
96378=C2=A0 com.apple.InputM 0.0=C2=A0 00:00.26
96376=C2=A0 AppleSpell=C2=A0 =C2=A0 =C2=A0 =C2=A00.0=C2=A0 00:00.68
96015=C2=A0 head=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A00.0=C2=A0 0= 0:00.00
#+end_example

This function will update that block. We increment a counter and cancel the= timer when it is exceeded.
#+BEGIN_SRC emacs-lisp
(defun update-src-block (fname src-name N)
=C2=A0 "Update the src-block named SRC-NAME in the file FNAME upto N t= imes."
=C2=A0 (if (> *counter* N)
=C2=A0 =C2=A0 =C2=A0 (progn (cancel-timer *timer*)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(message-box "Done wit= h timer"))
=C2=A0 =C2=A0 (save-excursion
=C2=A0 =C2=A0 =C2=A0 (with-current-buffer=C2=A0 (find-file-noselect fname)<= br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 (org-babel-goto-named-src-block src-name)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (org-babel-execute-src-block)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (incf *counter*)))))
#+END_SRC

#+RESULTS:
: update-src-block

We use a global var for the counter and timer.
#+BEGIN_SRC emacs-lisp
(setq *counter* 0)
(setq *timer* (run-with-timer
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0nil 5
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0'update-src-bloc= k
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"/Users/jkitchi= n/blogofile-jkitchin.github.com/_blog/blog.org"
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"time-string&qu= ot;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A05))
#+END_SRC

#+RESULTS:
: [nil 22153 41074 708648 5 update-src-block ("/Users/jkitchin/
blogofile-jkitchin.github.com/_blog/blog.org" &= quot;time-string" 5) nil 0]


Giri Prashanth writes:

> Hi,
>
> I would like to run a org-src block code periodically, is there any > property for org-blocks for such repeated execution.
>
> For example if I want to watch the tail of a file periodically every 1= min
> for next 1 hour, I would like to set a property like repeat for times = to
> repeat and keep the org file auto updated
>
> #+BEGIN_SRC shell :repeat 60 :repeat-every 60sec
> tail -n 10 file
> #+END_SRC
>
> Thank you,
> -GP

--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu



--
-giri
--001a11421ec4f0fce10528cdc0e2--