Named pipes - FIFO pipes.

Every day solutions to every day challenges. + Brilliant stuff

Moderators: b1o, jkerr82508

Forum rules
Please feel free to post your tip it does not have to be advanced. Also ask questions directly related to the tip here. But do not start new threads with questions or ask for help here. That is what the help section is for. forum rules: http://bjoernvold.com/forum/viewtopic.php?f=8&t=568
User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Named pipes - FIFO pipes.

Postby viking60 » 21 Jun 2014, 15:34

We have that regular pipes typically

Code: Select all

cat /var/log/somelog.log |less
It will show you the log per page. The named pipes or FIFO (First in First Out) pipes are a container ready to receive your instructions and is a file so you have to create it:

Code: Select all

mkfifo berserk_pipe

To put this file in receiver mode you can simply do a

Code: Select all

cat berserk_pipe

Now this terminal will be "frozen" waiting for messages.
Open another terminal next to the frozen one and type:

Code: Select all

echo "Hi Hola Hallo!!" >>berserk_pipe


The message will turn up in the other receiving terminal and it will go back to the normal prompt
Image

To make the receiving terminal remain in that "listening mode" you can start it with:

Code: Select all

tail -f berserk_pipe

Image
As you can see the left terminal will wait for any command or message. You can send regular commands too:

Code: Select all

ls >> berserk_pipe

Image
It can be useful when exchanging data between different users etc.

You can also use this in combination with SSH

Code: Select all

ssh berserk@10.0.0.12 "echo hallo >> berserk_pipe"


Remember berserk_pipe is just a file and you can have a look at it with:

Code: Select all

ls -l berserk_pipe

the output will be something like this:

Code: Select all

prw-r--r-- 1 berserk berserk 0 juni  21 16:15 berserk_pipe

Note the p in front it indicates that it is a pipe.

If you have started the pipe with tail -f you need to stop it with CTRL+C
The pipe file can be removed with a regular rm.

So what can this be used for? Use your imagination :-D I will come up with a practical example later...
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Named pipes - FIFO pipes.

Postby viking60 » 21 Jun 2014, 16:20

You can use FIFO pipes to control your mplayer.

Code: Select all

mkfifo ~/mplayer-control

then start the concert with whatever file you want to play:

Code: Select all

 mplayer -slave -input file=/home/berserk/mplayer-control FileToPlay.mp3


From another terminal you can start and stop the music with:

Code: Select all

echo "pause" >~/mplayer-control


Works like a charm and how could you ever live without it :whistle:
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"


Return to “Tips & Tricks”