On Unix, find if user who executed the program is root?

一曲冷凌霜 提交于 2019-12-21 07:10:09

问题


I'm writing a rake script and would like to detect (using Ruby rather than bash if possible) if the user who executed the rake script has root privileges.

If it is not root then I would like to terminate the script.


回答1:


Use uid or euid in the Process class:

raise 'Must run as root' unless Process.uid == 0



回答2:


I don't know Ruby, but what you want to check for is if the user ID is 0. In C, you would do this by checking getuid(). From the Unix command line, you could also check the output of id -u.



来源:https://stackoverflow.com/questions/641449/on-unix-find-if-user-who-executed-the-program-is-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!