我正在尝试将我的 SLES 12 系统设置为仅在第一次 root 登录到 SLES 12 系统时运行脚本。之后,root 将永远不会再次运行该脚本。此外,我不希望任何非 root 用户运行此脚本。
有人对我如何做到这一点有任何想法吗?
提前致谢。
如果脚本只执行一次是必要的,则标志文件可能会被意外删除,并在下次登录时触发重新运行。作为替代方案,考虑翻转条件,并可能使脚本不可执行,以防万一。
在系统配置期间,使用“/path/to/script”创建可执行文件“~root/run-once.sh”。
然后在 /etc/profile
if [ -x ~/run-once.sh ] ; then
source ~/run-once.sh
# Disable re-run with
rm ~/run-once.sh
# OR
chmod -x ~/run-once.sh
fi
作为副作用,如果需要,这种方法可以强制其他用户进行其他一次性登录。
一种方法是使用标志文件和测试条件。
script_function()
{
# what your script does
}
if [ -e /root/.flagfile ]
then
: # do nothing
else
script_function
touch /root/.flagfile
fi
然后,将脚本放在您的 profile.d 或 .bash_login 中,仅适用于 root 用户。