Score:0

How do I schedule an action to happen in 5 seconds?

by flag

I am in a situation that restarting the SSHD server along all it's open connection processes is required. So I thought that I might be able to use cron or at commands to schedule a service restart in a few seconds to the future (... and kill the process now). Problem is, both schedulers are not built for a few seconds schedules. For example: at now + 1 minute is a legit command while: at now + 5 seconds isn't a viable input. Any idea how to resolve that?

FedKad avatar
cn flag
I don't think these commands support granularity less that a minute. You can write your own script: using a loop with a `sleep` in between.
muru avatar
us flag
"(... and kill the process now)" which process?
rubmz avatar
by flag
@muru - "(... and kill the process now)" - I run: "kill -1 sshd" (kills all sshd processes and terminates any possibility of re-connecting until the SSHD is restarted.
rubmz avatar
by flag
@FedonKadifeli the problem is that the running script must be off another process / parent process than the SSHD which is how I run scripts on the server. Otherwise killing the sshd process will terminate that script too.
muru avatar
us flag
@rubmz if you do that, `systemd` should automatically restart SSH.
rubmz avatar
by flag
@muru I didn't happen as expected last time for either: 1. the service status was up while the process was terminated, 2. the system takes too long to identify the process closed/service stopped or too long to restart it (should happen as fast as possible, and for at least a minute it was down) 3. somehow the service is not configured for monitoring/auto restart.
muru avatar
us flag
None of those seem very probable. Which version of Ubuntu is this? Have you modified the ssh service? What does `systemctl status ssh.service` show?
danzel avatar
cn flag
Can't you simply `systemctl restart ssh`?
rubmz avatar
by flag
@muru This is ubuntu 18. I just pkilled my sshd service again and I am not seem to be able to reconnect again... I did check the systemd init file and changed its restart param from "Restart=on-failure" to "Restart=always". Rest of params seemed pretty standard (it's an AWS machine...)
rubmz avatar
by flag
@danzel sshd service is keeping it's processes up on purpose on restart. It's this way since it's easier to keep open connections open. The problem is that sometimes you do need to hard reset one of the connections (which our case is not very distinguishable which sshd process it is...)
Score:3
cn flag

Answering the original question:

Instead of scheduling a job to run in a few seconds, you can schedule it to run now and delay execution at the start of the job. For example:

echo "sleep 5; wall Job executed" | at now

Note that at's man page says about now:

Indicates the current day and time. Invoking at now shall submit an at-job for potentially immediate execution (that is, subject only to unspecified scheduling delays).

So it is possible that at now won't immediately execute the job, but at least on my 18.04 installation it does.

rubmz avatar
by flag
Nice and useful answer! So I marked it as the correct one.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.