[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Writing a looping, low-configuration script in Ruby

Francis Hwang

11/11/2004 1:45:00 PM

So I'm writing a one-file Ruby program that will generate an RSS feed
every half-hour, and I want it to have as little configuration as
possible. This is aimed both at Rubyists and newcomers who might be
pulled into the Rubyverse by another well-targeted tool, so I'm trying
to think of the clearest, easiest way that the user could configure
this.

So here's what I'm thinking: If this has to run every 30 minutes, the
easiest way (for me, the programmer) to do this, is have the user run
it as a backgrounded process:

$ ./script.rb &

but that's sort of fragile, particularly since (if I'm understand Unix
process correctly) backgrounding is handled in the shell so you can't
really log out without killing the background. Also, if you restart the
web server you lose the process, too.

So then I was thinking: Can I have a config that automatically edits
the crontab? Has anybody used Ruby to programmatically edit a crontab?
I suppose the brute force way to do it is to call "crontab -e" and send
the various keystrokes to stdin, but I wonder if anybody knows of a
less duct-tapey way.

F.



8 Answers

Florian Frank

11/11/2004 2:09:00 PM

0

On 2004-11-11 22:44:51 +0900, Francis Hwang wrote:
> So then I was thinking: Can I have a config that automatically edits
> the crontab? Has anybody used Ruby to programmatically edit a crontab?
> I suppose the brute force way to do it is to call "crontab -e" and
> send the various keystrokes to stdin, but I wonder if anybody knows of
> a less duct-tapey way.

You can install a new crontab with
$ crontab filename
or
$ crontab -
for stdin input.

You can output the current crontab of $USER with
$ crontab -l > filename

Of course you can do this in a ruby script, too. In this case you should
perhaps use a lock file.

If you only want to run a script every 30 minutes with cron, you
can add a line like
*/30 * * * * /path/to/script.rb
to your crontab. This is actually one of the main reasons to install
something like cron.

--
Florian Frank


Francis Hwang

11/11/2004 6:03:00 PM

0

Cool! Funny that stuff isn't in the man page. Thanks much.

On Nov 11, 2004, at 9:09 AM, Florian Frank wrote:

> On 2004-11-11 22:44:51 +0900, Francis Hwang wrote:
>> So then I was thinking: Can I have a config that automatically edits
>> the crontab? Has anybody used Ruby to programmatically edit a crontab?
>> I suppose the brute force way to do it is to call "crontab -e" and
>> send the various keystrokes to stdin, but I wonder if anybody knows of
>> a less duct-tapey way.
>
> You can install a new crontab with
> $ crontab filename
> or
> $ crontab -
> for stdin input.
>
> You can output the current crontab of $USER with
> $ crontab -l > filename
>
> Of course you can do this in a ruby script, too. In this case you
> should
> perhaps use a lock file.
>
> If you only want to run a script every 30 minutes with cron, you
> can add a line like
> */30 * * * * /path/to/script.rb
> to your crontab. This is actually one of the main reasons to install
> something like cron.
>
> --
> Florian Frank
>



Logan Capaldo

11/11/2004 7:57:00 PM

0

Well if you don't want to fiddle with crontabs and keep it
cross-platform, rather than doing ./script.rb & you could look into
Process.fork

something like

$ cat runevery30mins.rb
#!/usr/bin/env ruby

Process.fork do
loop
generate_rss_feed
sleep(30 * 60)
end
end

then simply

$ ./runevery30mins.rb
$

It will immediately exit but fork into another process that will
generate a new rss feed every 30 minutes

Obviously you may want to add some way of stopping it (short of killing it)
perhaps with Drb.


On Fri, 12 Nov 2004 03:03:17 +0900, Francis Hwang <sera@fhwang.net> wrote:
> Cool! Funny that stuff isn't in the man page. Thanks much.
>
>
>
> On Nov 11, 2004, at 9:09 AM, Florian Frank wrote:
>
> > On 2004-11-11 22:44:51 +0900, Francis Hwang wrote:
> >> So then I was thinking: Can I have a config that automatically edits
> >> the crontab? Has anybody used Ruby to programmatically edit a crontab?
> >> I suppose the brute force way to do it is to call "crontab -e" and
> >> send the various keystrokes to stdin, but I wonder if anybody knows of
> >> a less duct-tapey way.
> >
> > You can install a new crontab with
> > $ crontab filename
> > or
> > $ crontab -
> > for stdin input.
> >
> > You can output the current crontab of $USER with
> > $ crontab -l > filename
> >
> > Of course you can do this in a ruby script, too. In this case you
> > should
> > perhaps use a lock file.
> >
> > If you only want to run a script every 30 minutes with cron, you
> > can add a line like
> > */30 * * * * /path/to/script.rb
> > to your crontab. This is actually one of the main reasons to install
> > something like cron.
> >
> > --
> > Florian Frank
> >
>
>


Dave Burt

11/11/2004 8:52:00 PM

0

> "crontab -e"

> $ crontab -
> */30 * * * * /path/to/script.rb

> Process.fork do
> ...
> sleep...

This is a generally useful thing to do.

Is there any standard way to do this on windows? Is using win32/popen3
library the preferred way? How can you write a script that will work "out of
the box" (ie one-click-installer) on windows as well as doing the normal
unix thing on unix?

Cheers,
Dave


Logan Capaldo

11/11/2004 10:12:00 PM

0

I had thought that Process.fork works out of the box on windows, am I
wrong? I didn't really want to reboot to try it out. (Isn't laziness
supposed to be a virtue?) You could run the ruby interpreter under
Cygwin in windows and then I am (almost) positive it will work.


On Fri, 12 Nov 2004 05:58:29 +0900, Dave Burt <dave@burt.id.au> wrote:
> > "crontab -e"
>
> > $ crontab -
> > */30 * * * * /path/to/script.rb
>
> > Process.fork do
> > ...
> > sleep...
>
> This is a generally useful thing to do.
>
> Is there any standard way to do this on windows? Is using win32/popen3
> library the preferred way? How can you write a script that will work "out of
> the box" (ie one-click-installer) on windows as well as doing the normal
> unix thing on unix?
>
> Cheers,
> Dave
>
>


Dave Burt

11/12/2004 7:29:00 AM

0

> On Fri, 12 Nov 2004 05:58:29 +0900, Dave Burt <dave@burt.id.au> wrote:
>> > "crontab -e"
>>
>> > $ crontab -
>> > */30 * * * * /path/to/script.rb
>>
>> > Process.fork do
>> > ...
>> > sleep...
>>
>> This is a generally useful thing to do.
>>
>> Is there any standard way to do this on windows? Is using win32/popen3
>> library the preferred way? How can you write a script that will work "out
>> of
>> the box" (ie one-click-installer) on windows as well as doing the normal
>> unix thing on unix?

"Logan Capaldo" <logancapaldo@gmail.com> wrote...
>I had thought that Process.fork works out of the box on windows, am I
> wrong? I didn't really want to reboot to try it out. (Isn't laziness
> supposed to be a virtue?) You could run the ruby interpreter under
> Cygwin in windows and then I am (almost) positive it will work.


C:\WINDOWS>ruby -v
ruby 1.8.1 (2003-12-25) [i386-mswin32]

C:\WINDOWS>irb
irb(main):001:0> Process.fork {puts "I'm a child"}
NotImplementedError: The fork() function is unimplemented on this machine
from (irb):1:in `fork'
from (irb):1
irb(main):002:0> exit

Cygwin doesn't count.

What's against win32/popen3 being included in the one-click installer?

Cheers,
Dave


Curt Hibbs

11/12/2004 11:24:00 AM

0

Dave Burt wrote:
>
>
> What's against win32/popen3 being included in the one-click installer?

It probably should be. I'll put this on my todo list.

Curt


Kristof Bastiaensen

11/12/2004 11:53:00 AM

0

Hi,

On Thu, 11 Nov 2004 22:44:51 +0900, Francis Hwang wrote:
> <snip>
> So here's what I'm thinking: If this has to run every 30 minutes, the
> easiest way (for me, the programmer) to do this, is have the user run it
> as a backgrounded process:
>
> $ ./script.rb &
>
> but that's sort of fragile, particularly since (if I'm understand Unix
> process correctly) backgrounding is handled in the shell so you can't
> really log out without killing the background. Also, if you restart the
> web server you lose the process, too.
>

FYI, on unix you can start a process with nohup, so that logging out
will not kill the process.

KB