Created
January 11, 2019 07:09
-
-
Save sangheonhan/54ebfd2af0659a9cdff3bb7f68ef6ca9 to your computer and use it in GitHub Desktop.
Homebrew로 설치한 서비스 실행/중지 스크립트
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env perl | |
use strict; | |
use warnings; | |
our %services = ( | |
'nginx' => '~/Library/LaunchAgents/homebrew.mxcl.nginx.plist', | |
'php55' => '~/Library/LaunchAgents/homebrew.mxcl.php55.plist', | |
'gearman' => '~/Library/LaunchAgents/homebrew.mxcl.gearman.plist', | |
'memcached' => '~/Library/LaunchAgents/homebrew.mxcl.memcached.plist', | |
'mysql' => '~/Library/LaunchAgents/homebrew.mxcl.mysql.plist', | |
); | |
my $service; | |
my $command; | |
($service, $command) = parse_argv(@ARGV); | |
unless ( $service ) { | |
print "Usage: brewservice <service> <start|stop|restart>\n"; | |
print "\nService: ",join(' ', keys %services),"\n"; | |
exit 1; | |
} | |
my $plist = $services{$service}; | |
my $retval; | |
$command = lc($command); | |
if ( $command eq 'start' ) { | |
$retval = start_service($plist); | |
} elsif ( $command eq 'stop' ) { | |
$retval = stop_service($plist); | |
} elsif ( $command eq 'restart' ) { | |
stop_service($plist); | |
$retval = start_service($plist); | |
} | |
exit $retval; | |
sub parse_argv { | |
my @argv = @_; | |
if ( @argv != 2 ) { | |
return undef; | |
} | |
unless ( grep {/^$argv[0]$/i} keys %services ) { | |
return undef; | |
} | |
unless ( grep {/^$argv[1]$/i} qw/start stop restart/ ) { | |
return undef; | |
} | |
return ($argv[0], $argv[1]); | |
} | |
sub start_service { | |
my $plist = shift or return undef; | |
return system "launchctl load -w $plist"; | |
} | |
sub stop_service { | |
my $plist = shift or return undef; | |
return system "launchctl unload -w $plist"; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment