Writing a looping, low-configuration script in Ruby

F

Francis Hwang

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.
 
F

Florian Frank

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.
 
L

Logan Capaldo

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.


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

Dave Burt

"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
 
L

Logan Capaldo

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.
 
D

Dave Burt

Logan Capaldo said:
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
 
K

Kristof Bastiaensen

Hi,

<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top