简体   繁体   中英

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 . 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. 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 ; 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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