Sending keystroke to a process - linux

I want to write a seperate program that can receive commands from network and replay these commands to omxplayer. omxplayer is the video player on raspberry pi, we can control omxplayer via the keystroke. May anyone please suggest some approaches that we can send keystroke event to a running process?
Any suggestions are appreciated. Thanks!

You can use FIFO to send keystrokes to omxplayer.
We'll show you a basic example of how you do it.
In Shell (Terminal 1),
mkfifo /path/to/dir/fifo
omxplayer /path/to/movie/dir/movie.ext < /path/to/dir/fifo
after you execute these commands, the terminal 1 will hold.
Now in terminal 2
echo -n . > /path/to/dir/fifo
now it will start play.
This was the basic example. You can create a php file to write to the fifo file. So from that you can send commands.
p will pause
q will quit
also, when using non-letter commands (like up arrow, and down arrow), you should send the correct key code.
Hope this helps.

Related

Is it possible to programmatically read the `stdout` of another pty / tty without affecting its behavior?

I am attempting to programmatically read the output written by an application running in a specific tmux pane, so that I can determine when to send-keys to it from a controlling process.
In particular, I would like to automatically enter a password, but I do not want to enter it until I am sure the password prompt has appeared.
My current attempt has been to use tty to find the controlling tty, and then pass it to pyserial to try to read, since it appears to be able to read tty's. Note that in the real application, I have other ways of finding out the tty.
Unfortunately, as soon as I run the following code, the target tmux pane immediately closes.
import serial
ser = serial.Serial('/dev/ttys013', timeout=1)
Is it possible to read from a pty this way?
I am running on OSX, but would appreciate a solution that works on both Linux and OSX.
Typically, with python you would use pexpect to start the program and interact with it via a pty, but if you already have a program running in tmux you could simply use tmux's pipe-pane command to save a copy in a file of what is written to the screen. For example, for a pane number 1 you can give the shell command:
tmux pipe-pane -t 1 'cat >/tmp/capture'
and then tail the file /tmp/capture. (Use tmux pipe-pane -t 1 to stop).
To avoid polling, you can use a fifo instead.

If charger = unplugged, alert me. Command line script

I want to make a some kind of alert to let me know when the charger has disconnected (the cable doesn't fit into the socket well).
I have this to detect the status of the charger:
cat /sys/class/power_supply/ADP1/online
it returns 1 when charging and 0 when on battery.
and this to request a pop-up window:
zenity --info --text="Charger unplugged!"
I figured some kind of if statement would do the job, having to run it infinitely every second or so.
Then I remembered my days of Arduino where you could program an interrupt, that could trigger the script to run, rather than having to run it all the time.
Not sure what the best way to achieve this is. Does anybody know?
Many thanks.
You could create a service, or you can do it in a more simple and primitive way: create a script that will create a fork, redirect it's own stdout and stderr to /dev/null and execute an infinite loop that will be constantly checking your /sys/class/power_supply/ADP1/online file. When the value is 0 it will redirect the stdout and stderr to the original, in order to show the prompt. To make this an autonomous script, you would have to execute it with crontab #reboot.
I think that in the CLI it should work, but I don't know if it would work in a Graphical Desktop Interface

How to run linux application in background which uses shell?

I have an application/binary from a C program which by defaults uses the shell to take inputs from the user. So, when I start the application in background using & it stops automatically, because of the implementation on which I have no access. When I run this code
iStatus = system("./flute-static -send -a232.0.0.1/6666 a.txt &");
It gives output [1] 21970, the pid.
Then if I press another enter, it gives output
[1]+ Stopped ./flute-static -send -a232.0.0.1/6666 a.txt
And obviously it fails to send the data. How can I solve the problem. Please help me. Thanks in advance.
You can try nohup
iStatus = system("nohup ./flute-static -send -a232.0.0.1/6666 a.txt &");
Nohup means: do not terminate this process even when the stty is cut off.
Or You can use screen
https://www.mattcutts.com/blog/a-quick-tutorial-on-screen/
Read Advanced Linux Programming and about the fork system call; you surely want to use fork(2), execve(2), waitpid(2) with some other syscalls(2) and/or perhaps daemon(3) and/or popen(3). Perhaps using strace(1) on the flute-static program might help you understand more of it.
BTW, you might use some FLUTE library (compile MAD-ALCLIB from its source code!), or simply use an HTTP & FTP client library like libcurl
Whatever you do, if a backgrounded process is reading stdin, it is stopped (see signal(7), tty(4) etc...)! Read also the tty demystified

(Bash) Piping echo into node disables program keyboard input

So I am using node.js' built-in debugger and trying to skip the "break on first line" that it performs by passing in a delayed "c" keypress (c=continue) using echo, as such:
(sleep 1; echo -ne 'c\n') | node debug ~/src/main.js
It works as expected, but the node debugger no longer seems to accept keyboard input afterwords. I assume the piping in bash is doing something to cause node to ignore the keyboard. Anyone know how I can achieve the same result but maintain keyboard input to the node program?
Because node is being run in a pipeline, its input file descriptor is open to the pipeline, not to the console.
A common way to feed input to a process and then allow the user to interact is to use expect or pexpect.

Run a command in a shell and keep running the command when you close the session

I am using Putty to connect to a remote server. What I want to know is if there is any way to write my commands and allow them to keep running after I close the session with Putty. The reason for this is that I do not want to keep the computer ON all the time. Is there any way to do this?.
Update with the solution
For my question as it is presented the best solution is use one of the commands provided such as nohup, because you do not have to install any additional software. But if you are in the same problem use screen, install it and use it. It is amazing.
I have selected the answer of Norman Ramsey as favourite because propose several solutions using commands and screen. But please check the other answers specially the one of PEZ, then you get an insight of what screen is able todo.
screen! It's the best thing since sliced bread. (Yeah, I know others have already suggested it, but it's so good the whole world should join in and suggest it too.)
screen is like, like, ummmm ... like using VNC or the like to connect to a GUI destop, but for command shell windows. You can have several shell "windows" open at once in the same screen session. You can do stuff like:
Start a screens session using "screen -dR" (get used to using -dR)
run some commands in one window
press CTRL-A,C to create a new window open a file there in vim
press CTRL-A,0 to go back to the first window and issue some command on the file you just edited
CTRL-A, 1 to go back to your vim session
CTRL-A, C for yet another window and maybe do "sudo - su" (because you just happen to need a full root shell)
CTRL-A, 0 and start a background process
CTRL-A, C to create yet a new window, "tail -f" the log for that background process
CTRL-A, d to disconnect your screen then CTRL-D to disconnect from the server
Go on vacation for three weeks
Log on to the server again and issue "screen -dR" to connect to your existing screen session
check the log in the the fourth window with CTRL-A, 3 (it's like you've been there watching it all the time)
CTRL-A, 1 to pick up that vim session again
I guess you're starting to get the picture now? =)
It's like magic. I've been using screen for longer than I can remember and I'm still totally amazed with how bloody great it is.
EDIT: Just want to mention there's now also tmux. Very much like screen, but has some unique features, splitting the windows being the most prominent one.
nohup, disown, and screen are all good but screen is the best because unlike the other two, screen allows you to disconnect from the remote server, keep everything running, and then reconnect later to see what is happening. With nohup and disown you can't resume interacting.
Try using GNU Screen. It allows you to have several shells open at once. And you can disconnect from those running shells (i.e. close session with Putty) and they will keep doing their thing.
What you are looking for is nohup.
See the wiki link for how to use it.
screen is the best.
Try:
screen -dmS "MyTail" tail -f /var/log/syslog
This start command in background.
Use screen -r to list, and or screen -r Mytail to enter session.
If more users need access same session, use: screen -rx MyTail, and both or more users share the session.
If you can't use screen (because, for instance, your SSH session is being programmatically driven), you can also use daemonize to run the program as a daemon.
One way that works well for me is at.
at works like cron, but for a one-time job. I used it today to download a large file without having to keep my session alive.
for example:
$ at 23:55
at> wget http://file.to.download.com/bigfile.iso
at> ^D
You pass at a time (in the future) and it gives you a prompt. You enter the commands you want to run at that time and hit ctrl+d. You can exit out of your session and it will run the commands at the specified time.
Wikipedia has more info on at.
./command & disown
ssh localhost && ./command && exit

Resources