Abe Miessler
Abe Miessler

Reputation: 85056

Creating bash alias that starts two processes?

I would like to create an alias that:

  1. changes directories
  2. starts redis
  3. starts a node application

I was able to do this by creating an alias like the one below:

alias startSP='cd mydirectory; redis-server;node myApp.js'

The issue is that the node myApp.js pieces doesn't run until I kill redis. Is there some way that I can start two processes without waiting for the first to finish?

Should I be trying to open a second tab to do this?

Upvotes: 0

Views: 415

Answers (3)

RandomNumberFun
RandomNumberFun

Reputation: 642

Why not add a function to your profile instead?

startSP(){
 cd /your/directory
  redis-server && node myApp.js

}

The && will start make node myApp.js wait for the redis-server to start

Upvotes: 0

shellter
shellter

Reputation: 37288

I don't have a way to test this, but adding the '&' to run cmd in background should work, i.e.

 alias startSP='cd mydirectory ; redis-server & node myApp.js'

You will probably want to add a 2nd &, ie

 alias startSP='cd mydirectory ;redis-server & node myApp.js &'

As this will return the prompt to you and you can keep working at your commandline.

edit

There are many ways to kill processes, search here on S.O. If you lucky, you'll be able to do

   pkill redis-server
   pkill node ...? hm....

There are a lot of ways to get this wrong, so you'll do better to examine your particular case.

  • Are there other copies of redis-server running that you DON'T want to kill?
  • Are there other copies of node running AND then, how do you differniate those.
  • How do you make sure you're killing the 'myApp.js' version of node.

If I was going to do this all the time, I would turn startPS into a function that captured the PIDs returned from using the '&', saved those pids to the environment, and then had a seperate function that could confirm those PIDs existed, AND that they had a high probability of being the correct program, THEN issue the kill.

Note also, that kill -15 is the preferred way to kill a program, giving it the chance to close files, etc.

IHTH.

Upvotes: 3

ruakh
ruakh

Reputation: 183361

Instead of ;, which separates two processes sequentially, you might use &, which starts one process in the background and then runs the other in the foreground:

alias startSP='cd mydirectory ; redis-server & node myApp.js'

Upvotes: 2

Related Questions