0

A task/script scheduled with cron or at (AFAIK) can not run inside a specific terminal: it can only print the output (through redirection) of a task/script to a specific terminal.

Instead, I would like to schedule a command which should run in an already open terminal, as if I were typing the command there and then pressing Enter.

Is this possible at all?


I'm running Ubuntu 22.04, but if there's a solution I wish it is not dependent on the specific Linux version.


Some more details: I can run a script when I log into or I open the terminal, to print the current tty in a file. So the terminal is assumed to be always available.

The job should ignore the work I'm doing and just run, printing its output where the cursor is (if it overlaps with what I'm visualizing, it is ok). It does not need to acquire any input from the user: it just has a text output.

BowPark
  • 4,811
  • 12
  • 47
  • 74

1 Answers1

1

You could use any of the answers provided here Sleep until a specific time/date. I see no reason to copy here any of the solutions posted in the existing question.

You could also refer to the question posted at this SE website: Sleep until next occurrence of specific time

So, there are two options, either you want in background or foreground.

Foreground:

[birdie@localhost tmp]$ cat > test.sh
echo 123
[birdie@localhost tmp]$ chmod +x test.sh 
[birdie@localhost tmp]$ ( sleep 10; ./test.sh ) &
[1] 3689760
[birdie@localhost tmp]$ 
[birdie@localhost tmp]$ 
[birdie@localhost tmp]$ 
[birdie@localhost tmp]$ 123

[1]+  Done                    ( sleep 10; ./test.sh )

A little bit ugly but it works. It will pause if it wants to read anything from console because input is not there.

Just to feel safe, please run stty -tostop before any such commands. Here's an explanation why it's needed.

Or background:

nohup bash -c "sleep_command; command" &

If the particular sleep command is multiline, you could create a script or function out of it. nohup is required because otherwise your program will pause as soon as it will try to output anything.

Artem S. Tashkinov
  • 26,392
  • 4
  • 33
  • 64
  • Thank you. The problem here is that the terminal is "blocked" when `sleep` is counting down, instead if possible I would like to use it. – BowPark Mar 28 '23 at 10:11
  • If you use `nohup` the _stdout_ (and _stderr_) will get redirected to the file `nohup.out`, @ArtemS.Tashkinov. You could use `stty -tostop` to allow a backgrounded process to continue writing – roaima Mar 28 '23 at 10:25
  • @roaima I've provided both options. I wonder if it's possible for a background command to get keyboard input once it's been woken up. Probably not. – Artem S. Tashkinov Mar 28 '23 at 10:34
  • This looks like what I need. Thank you so much, for exploring all the cases. I'm interested in the background solution and there's no input to be waited from the user (I updated the question with this information), so it's the simplest case. – BowPark Mar 28 '23 at 10:44
  • 1
    nohup will work beautifully then. – Artem S. Tashkinov Mar 28 '23 at 10:48
  • @ArtemS.Tashkinov yes you can get input from a backgrounded job. It's not very pleasant :-O – roaima Mar 28 '23 at 10:56
  • Let's not dive that deep then :-D – Artem S. Tashkinov Mar 28 '23 at 10:57