Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command `at ` 5 seconds from now

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'. However, doing so would keep the ssh session alive until 'simulation/script' is done, which is undesirable to me.

I recently learned about the at command, and it seems to fit into my problem well.
What I want to do now is to ssh into my server, and at my simulation script to run in 5 seconds (more than enough time for the ssh connection to be closed). Thus, once the ssh connection is closed within 5 seconds, the server will start the simulation without needing the ssh connection to stay alive.

What I'm having trouble with is the time expression that at needs in order to schedule a job "5 seconds from now"

I have tried the following time expressions, all of which give me errors:

now + 5 seconds now + 5 sec now + 5 s now + 5seconds now + 5sec now + 5 s now+5sec now+5seconds now+5s 

How can I get my at to run my command "5 seconds from now"?

like image 507
inspectorG4dget Avatar asked Dec 16 '12 22:12

inspectorG4dget


People also ask

How do I run a Linux script in 5 seconds?

What you could do is write a shell script with an infinite loop that runs your task, and then sleeps for 5 seconds. That way your task would be run more or less every 5 seconds, depending on how long the task itself takes. You can create a my-task.sh file with the contents above and run it with sh my-task.sh .

How do I run a command after 5 minutes?

When you start up your computer press ctrl + alt + t and type amazon-sync then minimize the terminal window. Command will run once every 5 minutes (300 seconds).

How do I run a script at a specific time in Linux?

Usually in Linux you use crontab for this kind of scduled tasks. But you have to specify the time when you "setup the timer" - so if you want it to be configurable in the file itself, you will have to create some mechanism to do that. so how would you trigger that command? Crontab Guru (crontab.


2 Answers

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

echo "sleep 5 ; COMMAND" | at now 
like image 76
TomOnTime Avatar answered Sep 28 '22 09:09

TomOnTime


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 &' 
like image 43
Gilles Quenot Avatar answered Sep 28 '22 09:09

Gilles Quenot