Run a command for specific amount of time with an auto key press

Hi,

I have been trying to do a small fun project for myself.

I want to run a command for 45 seconds.

And to get the final output of this command, the script requires I push the "q" key on my keyboard and then the final output file becomes available.

I tried the following script. But it is not taking the "q" key and also runs for more than 45 seconds. I was wondering if someone could lead me on how to press a key automatically inside a bash script.

#! /bin/bash
end=$((SECONDS+45))
while [ $SECONDS -lt $end ];
do
my command execution
done
echo q

Thanks

Not sure if this is the optimal solution, and it requires "my command" to read from stdin:

mkfifo YY
{ sleep 45; echo q >YY; } &

read -p "my command execution" R <YY; [[ "$R" = "q" ]] && echo "back!" || echo "failed";    # this is mimicking "my command"
rm -f YY
1 Like

Rudic,

Thanks for your response. But no luck so far. The script runs but it is creating a file called YY and no output is being written into it.

To briefly tell you what I am planning to do

I want to execute a command for 45 seconds, then press the key "q". I want to loop this for 3600 seconds.

Thanks again.

Works for me, please provide screen output, like:

 tmp $ mkfifo YY
 tmp $ { sleep 45; echo q >YY; } &
[1] 30908
+ tmp $ 
 tmp $ read -p "my command execution" R <YY; [[ "$R" = "q" ]] && echo "back!" || echo "failed";    # this is mimicking "my command"
back!
[1]+  Fertig                  { sleep 45; echo q > YY; }
 tmp $ rm -f YY
1 Like

Please see this.

user$ mkfifo YY
user$ { sleep 45; echo q >YY; } &

[1] 33912

user$ read -p "ls -lh" R <YY; [[ "$R" = "q" ]] && echo "back!" || echo "failed";

-bash: !": event not found

user$ read -p "ls -lh" R <YY; [[ "$R" = "q" ]] && echo "back" || echo "failed";

back
[1]+ Done { sleep 45; echo q > YY; }

user$ rm -f YY

No output file is generated.

What is the output of:

uname -ro
$SHELL --version

I could only think of either one of these infos beeing related to your experienced issue, though i dont know a solution.

Obvious, after deleting it :wink:
You do not need that file anyway, as it simply 'redirects'(creates) the virtual keypress of q.

I do have it:

[1]+  Fertig                  { sleep 45; echo q > YY; }
+ tmp $ rm -f YY^C
130 tmp $ ll YY
prw-rw-r--. 1 sea sea 0 17. Mai 16:56 YY

hth

Thanks for the pointers. Here is the requested info

uname -ro

uname: illegal option -- o
usage: uname [-amnprsv]

user$ $SHELL --version

GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.

Anway, if you havent tried yet, start a new terminal window via gui-icon and retry RudiC's suggestion.
At some times, shell raises errors for standard stuff it supports, if one has been tinkering too much with it in previous tryouts...

But if there is no change, i'd guess bash beeing the reason, as there's been some updates of it:

$SHELL --version
GNU bash, Version 4.3.33(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL Version 3 or newer <http://gnu.org/licenses/gpl.html>

Sorry cant help you further.
Have a good day.

If I read your requirement correctly, this is a starter piece...
OSX 10.7.5, default bash terminal...

#!/bin/sh
# key.sh
# This will run as a timer and once a key-value, (line 8 in this code), is set then NOT pressing a key on the keyboard will run for $TIMER length.
# However if you want to exit pressing a key will exit immedaitely and not wait for the $TIMER to timeout...
# Pressing q manually will just rerun the command again IMMEDIATELY... 
TIMER=5
echo "q" > /tmp/YY
read key < /tmp/YY
echo "Character read is $key."
while true
do
	read -n1 -s -t$TIMER key
	if [ "$key" = "q" ]
	then
		echo "Run command(s) here."
	else
		break
	fi
done
echo "This is the key pressed:- $key."

Results:-
Note the second and third "Run command(s) here." was with a key press of "q"...

Last login: Sun May 17 18:32:39 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 key.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./key.sh
Character read is q.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
This is the key pressed:- e.
AMIGA:barrywalker~/Desktop/Code/Shell> _

@wisecracker - Thanks for a near good solution.

Your script still requests me to manually push the q button to generate the final output.

Here is an example for your reference.

  1. I want to get an image say - www.server.com/unix_is_nice.jpg
  2. I write a command -
wget www.server.com/unix_is_nice.jpg
  1. Now, I want to run this wget command for 45 seconds and then the jpg file becomes ready only if I push the "q" button on my keyboard.
  2. Writing step2 and the timer is fine for me. But my question is how to give the "q" input automatically in the bash script without me having to keep pushing "q" for every 45 seconds.

To keep everything in shape

while true
do
wget www.server.com/unix_is_nice.jpg #for 45 seconds
#push "q" button - This is what I want to know
sleep 45
done

Again I am really not sure why you want the 'q' character so:-
OSX 10.7.5, default bash terminal..

#!/bin/sh
# key.sh
echo "q" > /tmp/q
read key < /tmp/q
echo "Character read is $key."
# TIMER should be 45, set to 1 for testing.
TIMER=1
# OFFSET should be 3600, set to 5 for testing.
OFFSET=5
COUNT=$(($(date +%s)+$OFFSET))
while [ $(date +%s) -le $COUNT ]
do
	if [ "$key" = "q" ]
	then
		echo "Run command(s) here."
		read -n1 -s -t$TIMER key
	else
		echo "Exiting loop manually..."
		break
	fi
done
echo "You are here!"
echo "Character is $key..."

Results:-
First letting the loop take its course.
Second pressing any key for quick exit.
Third pressing the q key several times.

Last login: Sun May 17 21:50:57 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./key.sh
Character read is q.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
You are here!
Character is q...
AMIGA:barrywalker~/Desktop/Code/Shell> ./key.sh
Character read is q.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Exiting loop manually...
You are here!
Character is p...
AMIGA:barrywalker~/Desktop/Code/Shell> ./key.sh
Character read is q.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
You are here!
Character is q...
AMIGA:barrywalker~/Desktop/Code/Shell> _
1 Like

Thanks for your time @wisecracker. No luck. Let's move on.

THERE is your output! If that "back" was printed 45 seconds after you issued the snippet, it is EXACTLY what you were after. It proves that a "q" has been sent and received after the desired amount of time.