Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

runas - Run a program under a specific user / group (poor man's suexec)

#!/usr/bin/env ruby

# runas - Run another program under the privileges of a specified user and group.
# This is necessary because sudo demands a password, as we need it to be hands off.
# A poor man's suexec basically.

require 'etc'

user, group, cmd = ARGV

begin
  uid = Etc.getpwnam(user).uid
  gid = Etc.getgrnam(group).gid

  unless Process.euid == uid && Process.egid == gid
    Process.initgroups(user, gid)
    Process::GID.change_privilege(gid)
    Process::UID.change_privilege(uid)
  end

  exec cmd
rescue
  puts "Could not run as #{user}:#{group}"
  exit 1
end


Usage example: ./runas username groupname "sleep 10"

Daemonize a Ruby process

// description of your code here
from : http://scie.nti.st/2006/12/15/daemonize-a-ruby-process
Here's a neat thing I found. I needed a small bit of Ruby code to run continuously in the background, but because of how Capistrano can't seem to work well with "nohup" and "&" (background job), my ruby script itself needed to be able to fork and detach from the terminal. Here's how you do it:
#!/usr/bin/ruby

pid = fork do
  Signal.trap('HUP', 'IGNORE') # Don't die upon logout

  loop do

    // Some code

    sleep 60
  end
end

Process.detach(pid)

Daemonize a Ruby process

Here's a neat thing I found. I needed a small bit of Ruby code to run continuously in the background, but because of how Capistrano can't seem to work well with "nohup" and "&" (background job), my ruby script itself needed to be able to fork and detach from the terminal. Here's how you do it:

#!/usr/bin/ruby

pid = fork do
  loop do

    // Some code

    sleep 1.minute
  end
end

Process.detach(pid)


In my case, I just needed some code to run each minute, but it is part of a Rails app, so I didn't just want to start it with cron every minute (Rails loading is expensive). Neat eh?

#post method in tests with a different controller

I wanted a #login method in test_helper that would allow me to easily login from any of my functional tests. However, the #post method won't allow you to set a different controller than the one in the @controller instance variable that's defined in your test's #setup. Well, by looking at how the #process method works, you can see that it just grabs the controller from @controller. Redefine that, and you're good to go:
old_controller = @controller
@controller = LoginController.new
post(
  :attempt_login,
  {:user => {:name => 'joe', :password => 'password'}}
)
@controller = old_controller

If you have several login methods, such as a #login_admin and #login_regular, you could make a wrapper to reduce duplication:
def wrap_with_controller( new_controller = LoginController )
  old_controller = @controller
  @controller = new_controller.new
  yield
  @controller = old_controller
end

def login_admin
  wrap_with_controller do
    post(
      :attempt_login,
      {:user => {:name => 'root', :password => 'password'}}
    )
  end
end

def login_regular
  wrap_with_controller do
    post(
      :attempt_login,
      {:user => {:name => 'joe', :password => 'password'}}
    )
  end
end

search with grep in linux


Searching files

grep (r - recursive, s - no messages, n -line number, i - case insensitve, H -with filename)
grep -rsniH your_string

match lines containing the string "I am a cat" or the string "I am a dog".
grep "I am a \(cat\|dog\)" 


Search application

to find PID (process ID) of a certain application or process,
pgrep


find application port
netstat -a | grep ftp

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS