可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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"?
回答1:
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 &'
回答2:
"at" doesn't have sub-minute resolution but you can fake it:
echo "sleep 5 ; COMMAND" | at now
回答3:
at
doesn't use seconds, only minutes/hours/days
What you can do is precede your script with nohup
, which will ensure the script isn't killed when you disconnect your SSH session.
ssh server 'nohup yourscript.sh &'
NOTE: Having just played with the above, the SSH connection has to be killed manually.
Another alternative would be screen
screen -d -m yourscript.sh
This will launch a detached screen
process that you can reattach to at any time later.
NOTE: I've tested this with the following script and command and it worked perfectly.
SSH command
ssh server.com 'screen -d -m ~/myscript.sh'
myscript.sh
#!/bin/sh
sleep 10
echo "hello world" > /tmp/hello
exit;
回答4:
Just to note: in man at
, I saw there is a -t
switch, which will accept date times with seconds - but unfortunately the seconds will be truncated:
$ date; date --date="now +10 seconds" +"%m%d%H%M.%S"; echo "logger AAAA" | at -t $(date --date="now +5 seconds" +"%Y%m%d%H%M.%S")
Thu Feb 5 14:45:57 CET 2015
02051446.07
warning: commands will be executed using /bin/sh
job 8 at Thu Feb 5 14:46:00 2015
... and so the job may actually be scheduled in the past (also, used logger
to syslog, because it doesn't look like echoing to terminals' stdout can work from here)
回答5:
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.
回答6:
I think it is much easier doing:
sleep n && command
where n
is number of seconds.
回答7:
You can do it using sleep command like that:
bash -c 'sleep 5 ; echo "test"' &
回答8:
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'