Ruby CLI: Prompt for root password and continue executing script as root?
Other than requiring the user to invoke a ruby script through sudo
, is there a way to start running a script as a regular user, then at some point during runt开发者_JS百科ime, elevate privileges to root, by prompting the user for their root password?
Maybe something along the lines of #exec
using the current command prefixed with sudo?
This works. It just uses exec to call itself (test_script
in this case) again. But be very careful to make sure that it doesn't run infinitely by adding a condition which will call exit
.
#!/usr/bin/env ruby
if ARGV[0] == "--second"
puts "...called again and exiting."
exit
end
puts "Calling self again..."
exec "sudo ./test_script --second"
精彩评论