简体   繁体   English

如何在启动和关闭时启动和停止bash脚本?

[英]how to start and stop a bash script on startup and shutdown?

This is a script to monitor one file and notify changes 这是用于监视一个文件并通知更改的脚本

#!/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

i called the script filecheck.sh . 我叫脚本filecheck.sh Ran nohup ./filecheck.sh & and it ran without any problems, but it only runs until you shutdown the system, so i thought ill add this to /etc/init.d and i updated it. 跑了nohup ./filecheck.sh & ,它没有任何问题地运行,但是直到您关闭系统后它才运行,所以我以为我把它添加到/etc/init.d并更新了它。 But when i tried to reboot,the script kept on running and wasnt killed and i had to switch off my system. 但是,当我尝试重新启动时,脚本继续运行并没有被杀死,因此我不得不关闭系统。 Any help about how to kill the script on shutdown would be appreciated. 任何有关如何在关机时终止脚本的帮助将不胜感激。

You aren't actually running sha512sum ; 您实际上并没有运行sha512sum you need a command substitution for that. 您需要用命令替代。 The proper way to write this script: 编写此脚本的正确方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM