0

is there similar command to sleep, but where I can specify given time, so that my command will start at given time, INTERACTIVELY in my current open terminal window?

I know about at command, but that would not work as it does not run interactively in my current open terminal window.

I need to start my command at 04:00 in my terminal, as if I woke up, typed my command, and pressed enter.

The closest solution I can think of, is to calculate before I go to bed, how many seconds remain until 04:00, and use sleep command. But that is so ugly. There must be a better way to do this.

4
  • 3
    "As if I woke up" implies you will not be interacting with the terminal, so in what sense does the task need to be interactive? On the other hand, if you were using the terminal, what would you expect to happen to the existing foreground task (even a quiescent shell) when another foreground task was started? This smacks of an X-Y problem. Commented Apr 27, 2024 at 8:28
  • Consider using crontab Usage Commented Apr 27, 2024 at 8:29
  • @pico Since at will not work in starting a process inside an interactive terminal, why do you think cron will? Commented Apr 27, 2024 at 17:50
  • Each command of at and crontab is different. I can't see that crontab wouldn't work despite at. Commented Apr 28, 2024 at 6:39

1 Answer 1

1

If the "ugly" part is just calculating the sleep time, you can script it with four commands. I made them into a single command (with ; \) to avoid any typing delay, but you could script them instead. This does a single sleep -- no loops required.

The echo shows the sleep command: omit the word echo when you want the actual delay. The leading $ and > are shell prompts, not something you type.

$ Now=$( date '+%s' ); \
> End=$( date -d '0400 tomorrow' '+%s' ); \
> date -d "+ $(( End - Now )) seconds"; \
> echo sleep $(( End - Now ));
Sun 28 Apr 04:00:00 BST 2024
sleep 64036
$ 
1
  • 1
    I'm afraid it won't work as expected if this is launched "tomorrow" before 4.00. Minor test on (End-Now) > 24h to insert for sure. Nevermind, it is probably the best solution I could have thought about. Commented Apr 27, 2024 at 12:25

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.