Run command `at ` 5 seconds from now

前端 未结 8 1724
甜味超标
甜味超标 2020-12-29 05:03

As part of a slightly complex script, I need to tell a server to run a simulation. Normally, I would achieve this by doing ssh user@server \'simulation/script\'

相关标签:
8条回答
  • 2020-12-29 05:05

    You can do it using sleep command like that:

    bash -c 'sleep 5 ; echo "test"' &
    
    0 讨论(0)
  • 2020-12-29 05:11

    Redirecting stdin/stdout/stderr in addition to backgrounding the script will allow the SSH session to close immediately after executing the backgrounded command:

    ssh hostname "/path/to/script </dev/null >/dev/null 2>/dev/null &"
    

    Source: https://serverfault.com/a/36436/138334.

    0 讨论(0)
  • 2020-12-29 05:13

    "at" doesn't have sub-minute resolution but you can fake it:

    echo "sleep 5 ; COMMAND" | at now
    
    0 讨论(0)
  • 2020-12-29 05:14

    I think it is much easier doing:

    sleep n && command   
    

    where n is number of seconds.

    0 讨论(0)
  • 2020-12-29 05:17

    There's no seconds in at :

    man at said :

    • specification of a date must follow the specification of the time of day. You can also give times like now + count time-units, where the time-units can be minutes, hours, days, or weeks and you can tell at to run the job today by suffixing the time with today and to run the job tomorrow by suffixing the time with tomorrow.

    So instead of at, you could use a sleep I think.

    See man 1 sleep


    If you'd like to run ssh user@server 'simulation/script' without waiting, simply do :

    ssh user@server 'simulation/script' &
    

    the command will run in the background.

    Moreover, as Rawkode said, nohup will help there.

    So finally :

    nohup ssh user@server 'simulation/script' &
    

    with nohup, you can quit your terminal and have the ssh process alive.


    EDIT: if you want to run the ssh command and close the connection :

    ssh user@server 'simulation/script &'
    
    0 讨论(0)
  • 2020-12-29 05:18

    I ran into the same issue today, but I was able to resolve it using nohup

    nohup bash -c 'sleep 5 && at now -f script-name.sh'

    0 讨论(0)
提交回复
热议问题