Skip to content

Instantly share code, notes, and snippets.

@diegok
Last active January 19, 2020 23:41
Show Gist options
  • Save diegok/c53446a3eb3002fd9b031f7e0fb98066 to your computer and use it in GitHub Desktop.
Save diegok/c53446a3eb3002fd9b031f7e0fb98066 to your computer and use it in GitHub Desktop.
Script to handle bluetooth before and after systemd-suspend
#!/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;
}
@diegok
Copy link
Author

diegok commented Jan 19, 2020

This is the systemd service unit:

  • /etc/systemd/system/bluetooth-status.service
[Unit]
Description=Bluetooth sleep hook
Before=sleep.target
StopWhenUnneeded=yes

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=-/usr/local/bin/bluetooth-state suspend
ExecStop=-/usr/local/bin/bluetooth-state wakeup

[Install]
WantedBy=sleep.target

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment