了解 service 与 /etc/init.d 的区别以及引发的rsyslog服务重启不生效的问题

问题描述

情况描述

一台 RHEL 6 备机重启后,发现主机时间正确,但是 /var/log/message 与主机时间有差异,而且相差整整8小时。

初步分析

1、检查主机时间 date 时间正常
2、检查主机时区配置date -R 时区正常
3、检查应用日志时间,也正常
4、检查其他系统日志,发现cron/message/secure均存在时间差的问题
5、考虑是rsyslog没有正确加载到主机时间,可能是rsyslog启动以后修改过时间或者时区,导致系统日志时间异常,man rsyslogd 了解相关问题

如果不清楚为啥突然就定位到rsyslog,可以用另外一条思路 lsof 来定位是谁在写入这几个系统日志的文件

lsof /var/log/message

6、那就简单了,重启rsyslog应该就能恢复正常
7、发现 service rsyslog restart 不没解决问题 。。。。啥情况啊,总感觉RHEL 6有坑
8、 查看 service 文档和浏览相关博客,发现有人说 得 /etc/init.d/rsyslog restart 才会生效,试了一下,还真是
9、然后就发现当时时区修改配置是在/etc/profile 写入 TZ环境变量来实现的,然后就是service 执行时去掉了这个变量导致rsyslog时区配置咩有加载成功
10、为了避免重启后再次出现这个问题,更换一下时区配置方式
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

小结

(1)应该尝试重新配置一下时区,再去重启 rsyslog 服务的,不过这样就无法发现service的问题了,但是能较快的解决问题
(2)service 不加载环境变量我是没有想到的,这次解决这个问题真是碰巧,虽然RHEL 6 很旧了,但是也得记录一下这个坑,避免再犯类似错误。

帮助文档:

man service 节选

NAME
       service - run a System V init script

DESCRIPTION
       service  runs  a  System  V init script in as predictable environment as possible, removing most environment variables and with current
       working directory set to /.

       The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT.  The supported values of COMMAND  depend  on  the
       invoked script, service passes COMMAND and OPTIONS it to the init script unmodified.  All scripts should support at least the start and
       stop commands.  As a special case, if COMMAND is --full-restart, the script is run twice, first with the stop command,  then  with  the
       start command.

       service --status-all runs all init scripts, in alphabetical order, with the status command.

       If  the  init script file does not exist, the script tries to use legacy actions.  If there is no suitable legacy action found and COM‐
       MAND is one of actions specified in LSB Core Specification, input is redirected to the systemctl.  Otherwise  the  command  fails  with
       return code 2.

FILES
       /etc/init.d
              The directory containing System V init scripts.

ENVIRONMENT
       LANG, TERM
              The only environment variables passed to the init scripts.

帮助文档里面明确说明了,service命令是执行/etc/init.d 目录下 System V 初始化脚本,指定工作目录是 / ,另外会移除环境变量,仅保留LANG,TERM但是实际测试中,发现保留了如下环境变量,并没有LANG

export OLDPWD
export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PWD="/"
export SHLVL="1"
export SYSTEMCTL_IGNORE_DEPENDENCIES=""
export SYSTEMCTL_SKIP_REDIRECT=""
export TERM="xterm"

验证

如下的测试可以验证 service 命令去掉了环境变量,export可以看到service保留或者设置的环境变量

[root@localhost init.d]# more test
echo $TEST
echo $PATH
echo $LANG
echo $TERM
[root@localhost init.d]# export TEST='Hello World!'
[root@localhost init.d]# /etc/init.d/test
Hello World!
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
zh_CN.UTF-8
xterm
[root@localhost init.d]# service test start

/sbin:/usr/sbin:/bin:/usr/bin

xterm
[root@localhost init.d]# echo 'export' > test
[root@localhost init.d]# service test start
export OLDPWD
export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PWD="/"
export SHLVL="1"
export SYSTEMCTL_IGNORE_DEPENDENCIES=""
export SYSTEMCTL_SKIP_REDIRECT=""
export TERM="xterm"
补充

关于为啥上面脚本里面export执行是打印export 不是declare -x

1)我先尝试了一下,/bin/bash 执行是打印declare -x,然而/bin/sh执行是打印export
2)查看了一下,/bin/sh 软连接 指向 /bin/bash
3)然后搜了一下,大概意思是说虽然/bin/sh软连接指向/bin/bash 但是/bin/sh 等价于/bin/bash --posix
4)应该就是POSIX标准的差别了,看帮助文档吧

man bash 节选
       如果 bash 以名称 sh 启动,它试图模仿  (mimic)  sh  历史版本的启动过程,尽可能地相似,同时也遵循  POSIX  标准。  当作为交互式登录  shell
       启动时,或者是非交互但使用了  --login  选项  启动的时候,它首先尝试读取并执行文件 /etc/profile 和 ~/.profile, 中的命令.  选项 --noprofile
       用于避免这种行为.当使用命令        sh        来启动一个交互式的        shell         时,         bash         查找环境变量         ENV,
       如果有定义的话就扩展它的值,然后使用扩展后的值作为要读取和执行的文件          的名称.由于使用          sh          启动的          shell
       不会读取和执行任何其他的启动文件,选项 --rcfile 没有意义.使用名称 sh 启动的非交互的 shell 不会读取任何其他启动文件.当以 sh 启动时,  bash
       在读取启动文件之后进入 posix 模式.

       当   bash   以   posix   模式启动时,(和使用   --posix   命令行参数效果相同),它遵循   POSIX  标准.   这种模式下,交互式  shell  扩展  ENV
       环境变量的值,读取并执行以扩展后值为文件名的配置文件.  不会读取其他文件.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值