Results 1 to 5 of 5
I'm using virtual file-system in /proc/ to print out current working directory (CWD) :
Code:
ls /proc/$PID/cwd # part of an argument to printf (full printf below)
Now, some processes ...
- 12-12-2010 #1Just Joined!
- Join Date
- Dec 2010
- Posts
- 15
[SOLVED] Any way to know beforehand if SUDO is (going to be) needed?
I'm using virtual file-system in /proc/ to print out current working directory (CWD):
Now, some processes complain about SUDO privileges (e.g. init: ls: cannot access /proc/1/cwd: Permission denied).Code:ls /proc/$PID/cwd # part of an argument to printf (full printf below)
Is it possible to know beforehand if action/command will require SUDO? For example, is there a "maximum PID number", above which no process needs SUDO? Is the following snippet (that checks if user ran the script as SUDO) good enough for behorand-knowledge?Code:if [ $UID -eq 0 ] ; then # do something
BTW, in following printf snippet with 2 arguments,if ls returns error cannot open directory /proc/1/fd: Permission denied it outputs this error before "CWD:", like so:Code:printf "%15s %s\n" CWD: $((ls /proc/$PID/cwd) | tr '\n' ' ')
Why this happens?Code:ls: cannot access /proc/1/cwd: Permission denied CWD:Last edited by courteous; 12-12-2010 at 06:35 AM.
- 12-12-2010 #2
There is no way to know if a program requires sudo access before the program is actually run. This is because you do not know what files will be opened by a process until they are actually opened.
As for your question about the ls error output:
$(...) uses the STDOUT (fd 1) of the process that you are running. However, error output is usually printed to STDERR (fd 2) instead. Therefore, error output does not get picked up by $(...).
If you want the error output, you can run:
which redirects STDERR to point to STDOUT (and the $(...) will pick it up). If you always want to ignore the error output, you can run:Code:ls 2>&1
which will cause the error output to never appear.Code:ls 2> /dev/null
DISTRO=Arch
Registered Linux User #388732
- 12-12-2010 #3Just Joined!
- Join Date
- Dec 2010
- Posts
- 15
Cabhan, thank you for the reply.
And there is also no "max. PID number" above which there is no SUDO required? Anything else I could try?
Is it possible to check whether there was error ignored (with 2> /dev/null redirect) and, if it was, echo/print something else (say, "You need sudo privilege.")?
- 12-12-2010 #4
For your first question, there is no max PID or anything. PIDs are assigned randomly, as I recall, and again, there is no way to tell when a process is first spawned what files it will attempt to access.
"sudo access" is not a thing. All that sudo does is allow you to run a process as a user other than yourself. And there are no user-specific PIDs.
The way I see it, there are two possibilities:
1) Your program allows the user to tell it what to do in some instance, and the user may choose something that they are not allowed to do. In this instance, I believe that throwing an error is the correct behavior.
2) Your program requires access to information that is only root-accessible. In this instance, you know that you will require root privileges, and checking $UID is appropriate to ensure that you are running as root.
As for your question about checking if an error happened, you can do something like the following:
Here we execute ls, and save its output in a variable. We then check the $? variable, which contains the exit status of the last command. "0" means that the program executed successfully, while anything else indicates a failure of some sort. In this way, we can check whether the command worked.Code:ls_output=$(ls 2> /dev/null) if [ $? != 0 ]; then echo "ls failed to execute." >&2 fiDISTRO=Arch
Registered Linux User #388732
- 12-12-2010 #5Just Joined!
- Join Date
- Dec 2010
- Posts
- 15
Thank you very much.



