Ruby CLI: asks for the root password and continues to execute the script as root?

Besides the fact that the user must call the ruby โ€‹โ€‹script through sudo , is there a way to run the script as a regular user, then at some point during execution, increase the privileges to root by asking the user for their root password?

Could it be something like #exec strings using the current command with the sudo prefix?

+1
source share
1 answer

It works. It just uses exec to call itself ( test_script in this case) again. But be very careful to make sure that it does not work endlessly by adding a condition that 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" 
+2
source

Source: https://habr.com/ru/post/925782/


All Articles