Sending keyboard input to a running linux process

I am developing a web interface for an mp3 player (mpg123 linux). Mpg123 is a command-line mp3 player and can be controlled using keyboard inputs. For example:

$ mpg123 -C filename.mp3 

It will start playing songs and controlling keyboard inputs for control. Pressing 's' will stop the song "q" to exit, etc.

I am creating an mpg123 process using a Perl script. From this script, I want to send data for this process. I have a pid of a process, I just need to send keystrokes for this process for control purposes.

+6
linux perl keystroke
source share
1 answer

You just need to create your mp3 player as a pipe from perl. For example:

 $| = 1; # Set unbuffered output. open( my $mp3player, "| mpg123" ) or die "cannot start mp3 player: $!"; print $mp3player "s"; ... print $mp3player "q"; close $mp3player 

Second attempt for multiple script calls: in the interactive shell, enter tty . This will give you a pseudo terminal name. Now run the player in this shell. In another shell, write to this pseudo-terminal. For example. echo "s" > /dev/pts/11 . The player will receive this as an input. If this works, use your perl script instead of echo to write to the pseudo terminal.

+6
source share

All Articles