在命令上运行shell脚本

我想在使用date -s <string>命令时运行一个shell脚本。 例如,我想通过在shell脚本中执行以下命令将命令logging到文件/tmp/user.log

 logger -p user.notice "date -s command executed" -f /tmp/user.log 

如何在shell上执行date -s <string>时运行shell脚本?

为了使它更通用,我想在别人在我的系统上发出特定的linux命令时运行我的shell脚本。 这个怎么做?

改写这个问题,你想知道什么时候有人试图设置系统时间。 这正是audit子系统的用途……它允许您审计特定系统调用的执行情况。 在这种情况下,只要有人调用任何可以改变系统时间的系统调用,就想知道。 通过使用audit子系统,无论是否有人正在调用/bin/date或他们自己的本地构build版本的命令,都有一个解决scheme。

有关规则语法的完整说明,请参阅auditd(8)audit.rules(7) ,有关审计时间更改操作的示例,可以在示例nispom.rules文件中查找“时间更改”。 你可以在你的本地系统上find它,或者你可以在这里find它:

有关audit子系统的更多信息(和文档有点难):

您不能轻易地阻止用户运行他自己的程序版本,但是如果您认为用户不是恶意的,则很容易:

  1. 创build一个包装脚本,logging程序,然后运行它(使用原始参数)
  2. 确保包装器replace用户path中的原始程序。

您可以使用别名来使用户使用包装器,或者您可以简单地移动/重命名原始程序,并将包装器放在原始位置。

如果您只想logging一些执行,请在包装脚本中使用正则expression式。

非常感谢我的服务器serverfault朋友的答案。

我想我已经拿出了所有的帮助来回答我的问题。 但是,如果有任何与之相关的错误或任何改进措施,你们都会帮助我吗? 我来啦。

这些是我做的事情。

一世)。 用下面的代码在/ etc中创build了一个脚本datewrapper.sh

 #! /bin/bash # wrapper script for the date command. # whenever the date -s command is issued, it will be logged. # executing the date command first with parameter list if any date $@ # check whether the date command executed successfully if [ "$?" == "0" ] ; then for param in $@ ; do # if "-s" option is used, log it if [ "$param" == "-s" ] ; then # user.notice logs the message to /tmp/log/user.log # as per the commands in /etc/syslog-ng/syslog-ng.conf logger -p user.notice "New date set" break fi done fi exit 0 

ⅱ)。 chmod a + x /etc/datewrapper.sh; alias date ='/ etc / datewrapper.sh'

III)。 date

Tue Dec 21 21:51:01 UTC 2010

ⅳ)。 date-s“21:53:05”

Tue Dec 21 21:53:05 UTC 2010

V)。 我检查了/tmp/log/user.log。 它显示消息

12月21日21:53:05 localhost root:新的date设置

所以结果是每当用户给出date命令时,我的脚本就会被执行,每当他用-s选项发出命令,它就会login到/tmp/log/user.log