Last active
January 19, 2020 23:41
-
-
Save diegok/c53446a3eb3002fd9b031f7e0fb98066 to your computer and use it in GitHub Desktop.
Script to handle bluetooth before and after systemd-suspend
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 Mojo::Base -strict, -signatures; | |
use Mojo::File; | |
use Term::ANSIColor; | |
my $guard_file = Mojo::File->new('/tmp/bluetooth.status.guard'); | |
(sub($name) { | |
({ | |
wakeup => \&awake, | |
suspend => \&suspend, | |
status => \&status, | |
}->{$name||''} || \&status )->(')_-_('); | |
})->(shift); | |
sub suspend { | |
return if -f $guard_file; | |
return unless is_active(); | |
run('systemctl --no-page stop bluetooth.service'); | |
$guard_file->touch; | |
say "bluetooth.service stopped and saved guard."; | |
} | |
sub awake { | |
return unless -f $guard_file; | |
my $out = run('systemctl --no-page start bluetooth.service'); | |
$guard_file->remove; | |
say "bluetooth.service started and removed saved guard."; | |
} | |
sub status { | |
say is_active() ? colored("Bluetooth service is active", 'green' ) : colored("Bluetooth service is inactive", 'red'); | |
} | |
sub is_active { | |
run('systemctl --no-page is-active bluetooth.service') eq 'active'; | |
} | |
sub run($cmd) { | |
my $out = `$cmd`; | |
chomp($out); | |
$out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the systemd service unit: