One thing that has been puzzling me for some time is this:
% which halt
/sbin/halt
% file /sbin/halt
/sbin/halt: symbolic link to `reboot'
However, executing sudo halt
does, of course, not reboot the system. Why is that?
There are several other programs working that way, for example pdflatex
.
Best Answer
Every program can see the full command line that was used to run it (except for wildcards and variables, which the shell expands).
In a C program, the command line is stored in
argv
, which is short for argument vector.The progam's name is the first element of
argv
, i.e.argv[0]
.Clearly in the case of
halt
andreboot
, the program is changing its behavior based onargv[0]
.From bash, you can see the full command line used to run a program using
ps -p <pid> -o cmd
orcat /proc/<pid>/cmdline
.Note that there is another type of link called a hard link that will have the same effect. On my system for example,
sudo
andsudoedit
are the same file with two different names, and different behaviors.ls -i
can help you find those commands, e.g.:See
man ln
for more details about hard links if you're not familiar with them.