前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ubuntu 用户登录自启脚本

Ubuntu 用户登录自启脚本

作者头像
f_zyj
发布2021-12-07 16:50:01
1.4K0
发布2021-12-07 16:50:01
举报

Ubuntu 系统在用户登录时,会自动执行 /etc/profile,查看该脚本可以看到:

代码语言:javascript
复制
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

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

在最后几行脚本中,首先会判断是否存在 /etc/profile.d 文件夹,如果存在则会遍历该文件夹下的 *.sh 脚本并挨个执行。

所以想要在用户登录时自动执行脚本,不必修改 /etc/profile 脚本,当然修改这个脚本也是可以做到的,不过不推荐,只需要在 /etc/profile.d 文件夹下,写一个 x.sh 脚本即可,具体可以参考该文件夹下其他脚本,另外写完脚本以后,也无需对他修改权限,因为执行他的命令并不是 ./x.sh 的方式,而是 .\ x.sh ,相当于 sh\ x.sh

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档