1

I have this simple script to attempt to start 3 different Redis Sentinel processes that will monitor master/slave Redis processes:

# (attempts to run 3 sentinel processes from one script)
    redis-sentinel /usr/local/redis/sentinel-26379.conf
    redis-sentinel /usr/local/redis/sentinel-26380.conf
    redis-sentinel /usr/local/redis/sentinel-26381.conf
# end

but of course, this won't actually start 3 separate processes - it will just run the first command in the terminal window and the second two commands won't be run. What is the best way I can run all 3 commands in separate windows from one script? (I also have some other commands that I would like to run from the same script but I wanted to keep it simple for an example.)

I am using iTerm2 on Mac OSX.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

1

bash offers job control, meaning you can run multiple jobs at the same time, in background. Use & to start a process in background:

redis-sentinel /usr/local/redis/sentinel-26379.conf &
redis-sentinel /usr/local/redis/sentinel-26380.conf &
redis-sentinel /usr/local/redis/sentinel-26381.conf &

If you type

jobs

you'll get a list of running jobs. Note that all the jobs will write their output to the same terminal. I guess is what you really want.

If you really want to launch that processes in different windows, you need to start three terminals from your script and set the redis-sentinel command as their initial command. I don't know iTerm on OSX but I'm almost 100% sure that it offers such a functionality. For the gnome-terminal it would look like this:

 gnome-terminal -e 'redis-sentinel /usr/local/redis/sentinel-26379.conf' &

Check the man page to get the right option for iTerm.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • this is working great, but one more question for you - how do I kill/close all these jobs with one command - I had to do "ps auxww | grep -i redis" to see all the processes in order to kill them one by one. is there a way to cleanly kill them? – Alexander Mills Jul 06 '15 at 20:59
  • also "$> jobs" and "$> sudo bash jobs" didn't do anything, but your main suggestion seems to have worked great – Alexander Mills Jul 06 '15 at 21:00
  • to be more specific, the jobs command does work, but even when I opened a new terminal window, it returned nothing (not even "empty list"). "$> jobs -l" didn't list anything either. weird. – Alexander Mills Jul 06 '15 at 21:03
  • You can kill a job using it's job id, like this `kill %1`.. is this what you wanted to know? – hek2mgl Jul 06 '15 at 21:27
  • what I was trying to say was that the jobs command does work, but nothing is listed when I call '$> jobs', my only guess is because the '&' command at the end of each line doesn't do proper work of registered each line as a true job control job? – Alexander Mills Jul 06 '15 at 21:56
  • 1
    I would suggest to install GNU bash. The default OSX shell is quite old. – hek2mgl Jul 06 '15 at 22:05
  • I have iTerm2, also GitBash – Alexander Mills Jul 06 '15 at 22:15
  • Check this: http://apple.stackexchange.com/questions/69223/how-to-replace-mac-os-x-utilities-with-gnu-core-utilities – hek2mgl Jul 07 '15 at 07:38
  • Is there an effective, clean and safe way to kill all the processes that were started by the same script? – Alexander Mills Jul 08 '15 at 04:05
  • @AlexMills Does [this](http://stackoverflow.com/questions/360201/kill-background-process-when-shell-script-exit) help? – hek2mgl Jul 08 '15 at 08:54