/etc/profiled.d/ 中的 shell 脚本如何影响 /etc/profile

/etc/profiled.d/ 中的 shell 脚本如何影响 /etc/profile

我已经编写了一个 shell 脚本并将其放在中/etc/profile.d/

该脚本包含以下内容:

if [ ] # Boolean condition is in these brackets.
  then
    shutdown -P 23:00

因此,当我登录时,我收到一条以以下内容开头的消息:

Error found when loading /etc/profile:
Shutdown scheduled for ... 23:00:00 PDT, use 'shutdown -c' to cancel.
As a result the session will not be configured correctly.
You should fix the problem as soon as feasible.

这对我来说很奇怪,因为我没有对其进行任何更改/etc/profile

我的问题包括几个部分:

我是否应该对 作出更改/etc/profile

shutdown或者是我使用中存在的问题/etc/profile.d/

答案1

观察/etc/profile,我们看到:

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

也就是说,只要执行了,*.sh调用的脚本/etc/profile.d就会被执行。/etc/profile

何时/etc/profile执行?man bash(或在线手册页显示:

When  bash  is  invoked  as  an  interactive  login shell, or as a non-
interactive shell with the --login option, it first reads and  executes
commands  from  the  file  /etc/profile,  if  that  file exists.  After
reading that file, it looks  for  ~/.bash_profile,  ~/.bash_login,  and
~/.profile,  in  that  order,  and reads and executes commands from the
first one that exists and is readable.  The --noprofile option  may  be
used when the shell is started to inhibit this behavior.

和:

 If  bash  is  invoked  with  the name sh, it tries to mimic the startup
 behavior of historical versions of sh as  closely  as  possible,  while
 conforming  to  the  POSIX  standard  as  well.   When  invoked  as  an
 interactive login shell, or a non-interactive shell  with  the  --login
 option,   it   first   attempts  to  read  and  execute  commands  from
 /etc/profile and ~/.profile, in that order.  The --noprofile option may
 be used to inhibit this behavior. 

所有这些意味着每次您调用/etc/profile(上面显示了何时bash调用,您的调用是否正确~/.bashrc?)脚本都会运行。

在您第二次(以及后续)调用时/etc/profileshutdown看到另一个shutdown处于活动状态,并发出抱怨。

您可以在脚本中放入将旧的替换shutdown -c为新的。shutdown -P 23:00shutdownshutdown

相关内容