[sf-lug] What purpose is served by the first two parts? (cd / && umask 022 ...
Michael Paoli
Michael.Paoli at cal.berkeley.edu
Sun Jan 1 13:03:23 PST 2017
> From: "Alex Kleider" <akleider at sonic.net>
> Subject: Re: [sf-lug] server reboot
> Date: Sun, 01 Jan 2017 11:19:57 -0800
> On 2016-12-31 21:03, Michael Paoli wrote:
>> # (cd / && umask 022 && echo 'shutdown -r +2' | batch)
> What purpose is served by the first two parts?
> ( cd / && umask 022 )
cd /
at least many versions of shutdown will typically umount non-root
filesystems (at least before eventually dealing with root filesystem)
in order to umount those filesystems, they can't have any processes on
them with open files or current directories there - that's generally
dealt with by shutdown using fuser or the like to terminate/kill such
offending processes ... and if your shutdown itself didn't chdir(2) to
avoid that, then it may terminate itself.
umask 022
mostly precautionary - typically sufficiently but not excessive default
security for superuser (root) performing an operation such as shutdown,
software installation, etc.
()
do it in subshell, so it dosen't alter those outside of subshell
&&
from typical shell man page:
Short-Circuit List Operators
``&&'' and ``||'' are AND-OR list operators. ``&&'' executes the first
command, and then executes the second command iff the exit status of the
first command is zero.
Basically, if we don't want to continue if preceding command fails, &&
is quite useful.
E.g. novice/jr sysadmin does something in a crontab job like:
cd /some_log_directory; find . -mtime +30 -exec rm -f \{\} \;
And as root on a host where root's HOME directory is / - guess what
happened the first time that cd failed? Yes, && instead of ; would have
prevented that mess.
More information about the sf-lug
mailing list