-2

I Want to Use Key Combinations in Bash Scripting like After Starting Any server like PHP Script will Auto stop that server after some delay..

Ex: Php.sh

php -S 127.0.0.1:4444 -t /home/username/Desktop/PHP/ 
sleep 20 
CTRL + C or CTRL + Z

Can you Help me..?

MrVivek
  • 23
  • 7
  • 1
    Your question probably got downvoted because it is not clear enough. This is also the reason stated for a vote to close. Your code snippet is not a valid shell script and needs explanation. I suggest to read [ask]. The purpose of this site is not only to answer a question for you personally but also to provide a reference for others that may have similar question. – Bodo Apr 07 '21 at 13:21
  • Does this answer your question? [Timeout a command in bash without unnecessary delay](https://stackoverflow.com/questions/687948/timeout-a-command-in-bash-without-unnecessary-delay) – Tsyvarev Apr 07 '21 at 13:46

2 Answers2

0

The general solution here is to use shell job control, and signals that replicate the key-presses. php runs in the foreground, so you launch it like so in order to run it in the background:

php -S 127.0.0.1:4444 -t /home/username/Desktop/PHP/ &
pid=$!

The assignment captures the process id of php in a variable so it can be used later.

Then you can use the remainder of your script:

sleep 20

Then if you want to terminate it you can do:

kill $pid

If you want to suspend it you can do:

kill -STOP $pid

Together this would look like this if you wanted to kill it after 20 seconds:

php -S 127.0.0.1:4444 -t /home/username/Desktop/PHP/ &
pid=$!
sleep 20
kill $pid
wait

The wait pauses the script until the kill takes effect on the php process.

Petesh
  • 87,225
  • 3
  • 99
  • 116
  • Hey.. itz.. so easy thank you .. but can you tell me.. ? why i got down vote i dont know why this happened i lost my 2 reputations i'm new on stackoverflow .. can you tell me ? – MrVivek Apr 07 '21 at 13:06
  • I don't know the reason for the downvote. There is an element of a lack of clarity to the question - are you trying to simulate keypresses to an application, or are you attempting to get behavior. I was responding in relation to getting behavior – Petesh Apr 07 '21 at 13:27
0

i got one more way to use key combinations in bash scripting.. here need on tool called xdotool

sudo apt install xdotool -y

and then you can use key combinations in bash file

now the considering above example file will be like

php -S 127.0.0.1:4444 -t /home/username/Desktop/PHP/
sleep 20 
xdotool key ctrl+C
MrVivek
  • 23
  • 7
  • 1
    xdotool is for GUI based systems only - i.e. this will only work on the terminal in your GUI. In the case of the code you've written, php runs in the foreground, which means that if you wrote these three lines like this in a script, you will not get to the sleep until *after* the `php` process ends. – Petesh Apr 07 '21 at 13:25
  • yes, exactly happened with me, but how should face it.. ? – MrVivek Apr 08 '21 at 14:57
  • The issue seems to be an X-Y one. Are you asking 'how do I simulate keyboard input for a program', or are you asking 'how do I terminate/suspend a program after a period of time'? They have very different answers and approaches. Simulating keyboard input can be done with a tool like (expect)[https://en.wikipedia.org/wiki/Expect], which allows you to drive a program by wrapping the input/output. – Petesh Apr 08 '21 at 22:11