1

I created a script that will check for a directory update every 60 seconds and store it in a sleep.txt file until the directory stops being updated, at which point the script runs a bsub job. The problem with this is that I have to have a shell open running the check_directory.sh script so my computer must not sleep or turn off while this script is running. I was wondering if there is a way to do this in the background while I leave the computer? My current script is:

DIR_TO_CHECK='/dir/to/check'
OLD_CHECK='/home/sleep_file/sleep_file.txt'

if [ -e $OLD_CHECK ]
then
    OLD_FILE=`cat $OLD_CHECK`
else
    OLD_FILE="nothing"
fi

NEW_STAT=`stat -t $DIR_TO_CHECK`

if [ "$OLD_FILE" != "$NEW_STAT" ]
then
    echo 'Directory is different.'
    while true
    do
        echo "sleeping for a bit"
        sleep 60
    done
fi

bsub "...."

the bsub portion just submits my job when the first portion is done. Thanks and any help would be appreciated.

d_kennetz
  • 5,219
  • 5
  • 21
  • 44
  • So the question is "how can I run my program in the background"? Not specific, necessarily, to bash? – Charles Duffy Sep 11 '17 at 16:15
  • The best answer to that is "use the process supervision system your OS provides". What that process supervision system **is** depends on your operating system. – Charles Duffy Sep 11 '17 at 16:15
  • On MacOS, it's launchd. On most modern Linux distros, it's systemd. On slightly older Debian or Ubuntu, it's Upstart. – Charles Duffy Sep 11 '17 at 16:16
  • @Lety, starting a script in the background by hand is not ideal. A proper supervised daemon will be automatically restarted if it shuts down unexpectedly, started up on boot, etc. And the above doesn't detach stdin from the TTY or ignore HUP signals (which you *don't* need `nohup` for -- the bash builtin `detach` command can do the job). – Charles Duffy Sep 11 '17 at 16:17
  • @CharlesDuffy thanks charles, I hadn't seen the duplicate you recommended and that did exactly what I was asking. Sorry for the dupe. – d_kennetz Sep 11 '17 at 16:26

0 Answers0