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.

0

1 Answer 1

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.

7
  • 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 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 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. Mar 28 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 at 10:44
  • 1
    nohup will work beautifully then. Mar 28 at 10:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .