【问题标题】:how to start and stop a bash script on startup and shutdown?如何在启动和关闭时启动和停止 bash 脚本?
【发布时间】:2017-03-27 16:57:04
【问题描述】:

这是一个监控一个文件并通知更改的脚本

#!/usr/bin/env bash

update_sha()
{
 sha="sha512sum /bin/myfile"
}
update_sha

previous_sha=$sha
compare()
{
  update_sha
  if [[ $sha != $previous_sha ]] ; then
  notify-send "change detected"
  build
  previous_sha=$sha

  fi
}

trap exit SIGQUIT

while true; do
compare
sleep 1
done

我将脚本称为filecheck.sh。运行nohup ./filecheck.sh &,它运行没有任何问题,但它只运行到你关闭系统,所以我想把它添加到/etc/init.d,我更新了它。但是当我尝试重新启动时,脚本继续运行并且没有被杀死,我不得不关闭我的系统。任何有关如何在关机时终止脚本的帮助将不胜感激。

【问题讨论】:

标签: linux bash shell ubuntu-14.04


【解决方案1】:

你实际上并没有在运行sha512sum;你需要一个命令替换。编写此脚本的正确方法:

get_sha()
{
 sha512sum /bin/myfile
}

previous_sha=$(get_sha)
compare()
{
  current_sha=$(get_sha)
  if [[ $current_sha != $previous_sha ]] ; then
    notify-send "change detected"
    build
    previous_sha=$currentsha 
  fi
}

trap exit SIGQUIT

while true; do
  compare
  sleep 1
done

【讨论】:

  • 我也跑了这个,我把它添加到/etc/init.d它仍然不允许系统关闭,我认为它与信号有关
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 2012-09-05
  • 2016-03-19
  • 1970-01-01
  • 2014-07-07
  • 2020-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多