bash script to run in 5 minutes
Asked Answered
D

6

21

I just want a bash script to run 5 minutes after it's called. What am I doing wrong?

I have the command:

/path/to/my/script | at now + 5 min

And yet the script runs right away every time.

Dall answered 18/10, 2010 at 23:23 Comment(0)
D
53

You are executing the script immediately and sending its output into at. You need to send the name of the script itself into at:

echo /path/to/my/script | at now + 5 min
Dogoodism answered 18/10, 2010 at 23:26 Comment(0)
G
33

how about:

sleep 300 && /path/to/my/script
Genitourinary answered 18/10, 2010 at 23:26 Comment(4)
Yeah, this question didn't really need at. I'd suggest you use && instead of ;, so it can be cancelled.Dogoodism
at is more graceful imo as it does not hang a process.Lomax
yep, I use this one for shutdown sleep 300 && shutdown -h nowKone
using seep for shutdown makes no sense because shutdown itself has a convenient timer integrated shutdown -P +5Iodine
P
7
at -f /path/to/my/script -t now +5 minutes

This should work as far as scheduling a script to run at a specific time. For any more information on the "at" command try linuxmanpages.com. I may be wrong thought ( currently not at a linux system to test ).

Good luck anyways

Paralysis answered 18/10, 2010 at 23:43 Comment(0)
L
2

The problem is you're running the script and piping the output to the at command. What you need to do is run the at command with the path to your script as a parameter. I'm not sure of the syntax, but at -h or man at should help.

Lemal answered 18/10, 2010 at 23:28 Comment(0)
A
1

Commands are evaluated left to right, so first your script gets executed, the output of it will be piped to the at command, this is normal behaviour. Have look at at the at man pages for more information.

Adulation answered 18/10, 2010 at 23:27 Comment(0)
C
0

try this

sys.scheduled_run /path/to/my/script 5

main function

function sys.scheduled_run(){
    local PATH_TO_ACTION MINS SLEEPTIME
    PATH_TO_ACTION=$1
    MINS=$2
    SLEEPTIME=$(($MINS * 60))
    echo "Sleeping for $MINS minute ($SLEEPTIME seconds) and then running $PATH_TO_ACTION"
    ui.countdown $SLEEPTIME
    $PATH_TO_ACTION
    echo "Done"
    if [ "REPEAT" == "$3" ] 
    then
        echo "Going for Repeat"
        sys.scheduled_run "$@"
    fi
}

countdown function

function ui.countdown(){ #USAGE ui.countdown 60 countdown for 60 seconds
        local SECONDS=$1
        local START=$(date +%s)
        local END=$((START + SECONDS))
        local CUR=$START
        while [[ $CUR -lt $END ]]
        do
                CUR=$(date +%s)
                LEFT=$((END-CUR))

                printf "\r%02d:%02d:%02d" \
                        $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

                sleep 1
        done
        echo "        "
}
Curzon answered 21/5, 2011 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.