开发者

Ruby CLI: Prompt for root password and continue executing script as root?

开发者 https://www.devze.com 2023-04-05 23:17 出处:网络
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 ro

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

精彩评论

暂无评论...
验证码 换一张
取 消