user2420525
user2420525

Reputation: 1

Automate a Ruby command without it exiting

This hopefully should be an easy question to answer. I am attempting to have mumble-ruby run automatically I have everything up and running except after running this simple script it runs but ends. In short:

I want the below script to run automatically via a cronjob at a specified time and not end until the server shuts down.

#!/usr/bin/env ruby

require 'mumble-ruby'

cli = Mumble::Client.new('IP Address', Port, 'MusicBot', 'Password')
cli.connect
sleep(1)
cli.join_channel(5)
stream = cli.stream_raw_audio('/tmp/mumble.fifo')
stream.volume = 2.7

print 'Press enter to terminate script';
gets
cli.disconnect

Upvotes: 0

Views: 376

Answers (3)

Grant Birchmeier
Grant Birchmeier

Reputation: 18484

Assuming you are on a Unix/Linux system, you can run it in a screen session. (This is a Unix command, not a scripting function.)

If you don't know what screen is, it's basically a "detachable" terminal session. You can open a screen session, run this script, and then detach from that screen session. That detached session will stay alive even after you log off, leaving your script running. (You can re-attach to that screen session later if you want to shut it down manually.)

screen is pretty neat, and every developer on Unix/Linux should be aware of it.

How to do this without reading any docs:

  • open a terminal session on the server that will run the script
  • run screen - you will now be in a new shell prompt in a new screen session
  • run your script
  • type ctrl-a then d (without ctrl; the "d" is for "detach") to detach from the screen (but still leave it running)

Now you're back in your first shell. Your script is still alive in your screen session. You can disconnect and the screen session will keep on trucking.

Do you want to get back into that screen and shut the app down manually? Easy! Run screen -r (for "reattach"). To kill the screen session, just reattach and exit the shell.

You can have multiple screen sessions running concurrently, too. (If there is more than one screen running, you'll need to provide an argument to screen -r.)

Check out some screen docs!

Here's a screen howto. Search "gnu screen howto" for many more.

Upvotes: 3

lurker
lurker

Reputation: 58244

Lots of ways to skin this cat... :)

My thought was to take your script (call it foo) and remove the last 3 lines. In your /etc/rc.d/rc.local file (NOTE: this applies to Ubuntu and Fedora, not sure what you're running - but it has something similar) you'd add nohup /path_to_foo/foo 2>&1 > /dev/null& to the end of the file so that it runs in the background. You can also run that command right at a terminal if you just want to run it and have it running. You have to make sure that foo is made executable with chmod +x /path_to_foo/foo.

Upvotes: 1

Senjai
Senjai

Reputation: 1816

Use an infinite loop. Try:

while running do
  sleep(3600)
end

You can use exit to terminate when you need to. This will run the loop once an hour so it doesnt eat up processing time. An infinite loop before your disconnect method will prevent it from being called until the server shuts down.

Upvotes: 1

Related Questions