2

I use pianobar player. But before launching it I need to run tor and polipo. Both are continuous (are running until interrupted). Basically what I'm looking for is a single command which would spawn tor and polipo processes (their output is not needed) and then open pianobar in foreground.

edit: turns out tor takes several seconds to start, this is why suggested solutions didn't work at first. I post this in case anyone has the same problem:

function piano {
    tor &
    polipo &
    sleep 3
    pianobar &&
    killall tor
    killall polipo
}
  • You don't want tor and polipo to continue running? You can configure them as daemons, then you don't need to kill them. Killing them would stop them, no? – Kusalananda Jul 10 '16 at 6:51
  • I want all of this to start and stop every time. This is the reason I went to all this trouble. I have homebrew, so I can run brew services start tor one time and it will launch on startup. – seeker_of_bacon Jul 10 '16 at 7:02
  • But killall tor would kill that service, right? – Kusalananda Jul 10 '16 at 7:05
  • Of course. Until next startup. But the purpose of this function is to substitute service with temporary process. There is no point in running the service if there's such a function. If if I wanted tor to be active all the time, there's no point for this function. – seeker_of_bacon Jul 10 '16 at 7:16
2

put this in your .profile or .bashrc or .zshrc or whichever shell you use:

function mycommand {
    tor &
    polipo &
    planobar
}

And now just run mycommand

7

This is what shell scripts are for; to run several commands while only typing one. The following is a short script that I believe does what you asked for:

#!/bin/sh

tor    & tor_pid=$!
polipo & polipo_pid=$!

sleep 5
pianobar

kill $tor_pid
kill $polipo_pid

Put that in a file, maybe $HOME/local/bin/startpiano, make it executable with chmod +x $HOME/local/bin/startpiano, and add $HOME/local/bin to the end of your PATH variable.

This is also what shell functions are for; to perform a logically distinct task. You may create a shell function like this:

startpiano () {
  tor    & tor_pid=$!
  polipo & polipo_pid=$!

  sleep 5
  pianobar

  kill $tor_pid
  kill $polipo_pid
}

This would go in one of the startup files for your particular shell.

EDIT: With the edited question, I'd advise against using an alias as the task now have become a bit too complex. I'll leave my original text about aliases here intact though.

This is also what aliases are for; to do simple things you don't want to have to type out again and again. This is how you set of an alias for it:

alias startpiano="polipo &; tor &; pianobar"

This, too, would go in one of your shell's initialization scripts.

Any one of these three solutions will allow you to type

$ startpiano

to start the programs you need.

  • The shell script has the benefit over the shell function and the alias that it does not add more weight to your shell initialization files (apart from appending its location to the end of the PATH variable).

  • The shell function has the benefit over the alias that you later could add command line arguments to it (goes for the script too), and that it's readable (and in many ways behaves just like its own "script").

  • The alias works, but really, the only use of aliases is to replace really simple things with shorter things for the benefit of your fingertips, like command line flags to ls etc. (alias ls="ls -F" is the only alias I have).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.