Shell script spawning a process after a delay

后端 未结 4 559
梦谈多话
梦谈多话 2021-02-07 00:17

How can I spawn a process after a delay in a shell script? I want a command to start 60 seconds after the script starts, but I want to keep running the rest of the script withou

相关标签:
4条回答
  • 2021-02-07 00:26

    Alternative:

    echo 'echo "A"' | at now + 1 min
    

    If somehow the OS is not running at the specified time, the command will be executed as soon as it is available.

    0 讨论(0)
  • 2021-02-07 00:27

    & starts a background job, so

    sleep 60 && echo "A" &
    
    0 讨论(0)
  • 2021-02-07 00:38

    Can't try it right now but

    (sleep 60 && echo "A")&

    should do the job

    0 讨论(0)
  • 2021-02-07 00:42

    A slight expansion on the other answers is to wait for the backgrounded commands at the end of the script.

    #!/bin/sh
    # Echo A 60 seconds later, but without blocking the rest of the script
    
    set -e
    
    sleep 60 && echo "A" &
    pid=$!
    
    echo "B"
    echo "C"
    
    wait $pid
    
    0 讨论(0)
提交回复
热议问题