svc: add list, enable, disable abilities
This commit is contained in:
54
svc
54
svc
@@ -1,5 +1,11 @@
|
|||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
|
|
||||||
|
# runit sv utility ignores evironment variable SVDIR
|
||||||
|
# and unable to list services, fully relying to user this knowlege
|
||||||
|
|
||||||
|
# TODO handle root user case as system mode!
|
||||||
|
# TODO handle system mode correctly
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings "all";
|
use warnings "all";
|
||||||
use JSON::PP;
|
use JSON::PP;
|
||||||
@@ -30,9 +36,49 @@ if ($c->{mode} eq 'user') {
|
|||||||
chdir $c->{dir};
|
chdir $c->{dir};
|
||||||
}
|
}
|
||||||
|
|
||||||
help unless (defined($ARGV[0]) || defined($ARGV[1]));
|
unless(defined($ARGV[0])) {
|
||||||
|
help;
|
||||||
|
} else {
|
||||||
|
if ($ARGV[0] ne 'list') {
|
||||||
|
help unless (defined($ARGV[1]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ((-l "./$ARGV[0]") || (-d "./$ARGV[0]")) {
|
if ($ARGV[0] eq 'list') {
|
||||||
|
my $d;
|
||||||
|
|
||||||
|
if (defined($ARGV[1]) && $ARGV[1] eq '--all') {
|
||||||
|
$d = "$ENV{HOME}$c->{servicedir}";
|
||||||
|
} else {
|
||||||
|
$d = "$ENV{HOME}$c->{dir}";
|
||||||
|
}
|
||||||
|
|
||||||
|
opendir(D, $d) or die "Unable to list services\n";
|
||||||
|
print join("\n", sort(grep {!/^\.{1,2}$/} readdir(D))) . "\n";
|
||||||
|
closedir D;
|
||||||
|
} elsif ($ARGV[1] eq 'disable') {
|
||||||
|
my $d = "$ENV{HOME}$c->{dir}/$ARGV[0]";
|
||||||
|
|
||||||
|
if (-l $d) {
|
||||||
|
unlink("$d") or die "Unable to disable $ARGV[0]\n";
|
||||||
|
} else {
|
||||||
|
die "No such service $ARGV[0]\n";
|
||||||
|
}
|
||||||
|
} elsif ($ARGV[1] eq 'enable') {
|
||||||
|
my $d = "$ENV{HOME}$c->{dir}/$ARGV[0]";
|
||||||
|
|
||||||
|
if (-l $d) {
|
||||||
|
die "Service $ARGV[0] already enabled\n";
|
||||||
|
} else {
|
||||||
|
my $svcdir = "$ENV{HOME}$c->{servicedir}/$ARGV[0]";
|
||||||
|
|
||||||
|
if (-d $svcdir) {
|
||||||
|
symlink($svcdir, $d) or die "Unable to enable service $ARGV[0]\n";
|
||||||
|
} else {
|
||||||
|
die "No such service $ARGV[0]\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} elsif ((-l "./$ARGV[0]") || (-d "./$ARGV[0]")) {
|
||||||
chdir "./$ARGV[0]";
|
chdir "./$ARGV[0]";
|
||||||
die "Is service running?\n" unless (-d './supervise');
|
die "Is service running?\n" unless (-d './supervise');
|
||||||
chdir './supervise';
|
chdir './supervise';
|
||||||
@@ -79,7 +125,8 @@ if ((-l "./$ARGV[0]") || (-d "./$ARGV[0]")) {
|
|||||||
exit 0;
|
exit 0;
|
||||||
|
|
||||||
sub help {
|
sub help {
|
||||||
print "Usage $0 SERVICE [start|stop|restart|status]\n";
|
print "Usage $0 SERVICE [start|stop|restart|status|enable|disable]\n";
|
||||||
|
print " $0 list [--all]\n";
|
||||||
exit 0;
|
exit 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,5 +144,4 @@ sub status {
|
|||||||
close S;
|
close S;
|
||||||
return $status;
|
return $status;
|
||||||
}
|
}
|
||||||
|
|
||||||
# vim: set ft=perl noet ai :
|
# vim: set ft=perl noet ai :
|
||||||
|
|||||||
Reference in New Issue
Block a user