1

I have the habit of quickly setting some reminders with:

sleep $duration; notify-send $reminder_message

But lately I have had to move around and often change desks. I noticed that my $reminder_message started arriving later than intended. The culprit, it turn out is: that when I close lid then it suspends everything along with my sleep command.

Here's how I found out:

date; echo $duration; date -d "+$duration sec"; sleep "$duration"s; date

If I close my lid in between the above command then: date and date -d "+$duration sec" don't match. However if lid is not closed then they match!

So for a quick-fix I have disabled suspend with lid-close with:

HandleLidSwitch=ignore

(following this ask-ubuntu answer)

Now my sleep commands work as I wanted them to. But disabling suspend altogether I feel is a too aggressive fix and an overkill!

Is there a simpler solution? I just want that my sleep commands shouldn't be interrupted when I close my lid.

Inspired_Blue
  • 601
  • 2
  • 9
  • 26

1 Answers1

2

No offense, but we may have an XY problem here. Your impression that the solution to your real problem is with sleep may be wrong. sleepchecks until the specified time has passed, and that time stops when the computer sleeps.

Instead consider using the at command to schedule specific jobs to happen at a specific time.

at works like this:

echo "command_to_be_run" | at +30 minutes

will schedule a job 30 min later.

at is a lot like cron, but a job is run only once. It is not installed by default, but can be installed with sudo apt install at.

vanadium
  • 88,010
  • What is my second attempt? Did you mean this: date; echo $duration; date -d "+$duration sec"; sleep "$duration"s; date? Oh - this was just for testing. I wanted to verify the hypothesis if indeed sleep is interrupted if lid is closed. That is not my second attempt. – Inspired_Blue Sep 08 '22 at 07:26
  • I had considered using at command at some point. But I can't recall now why I didn't like it. Let me see if I can recall and get back. If I can't I will accept your answer. – Inspired_Blue Sep 08 '22 at 07:29
  • One of the issues with at was that I can't do at +2 min 15 sec. If I remember correctly, there is no support for handling duration in seconds in at. Is that correct? Yes - that is correct: stackoverflow answer uses sleep with at for seconds – Inspired_Blue Sep 08 '22 at 07:50
  • I can't recall fully - but I had trouble with commands that need sudo and using it with at. But I am not sure if that was just me or if there was some genuine compatibility problem. – Inspired_Blue Sep 08 '22 at 07:57
  • I see with respect to "second attempt": I removed that paragraph. Indeed, no seconds, but I do not see this relevant for your current use case. – vanadium Sep 08 '22 at 08:35
  • Yes true. For notifications at works perfectly fine! But there are other places where I have used sleep. I didn't know sleep had this feature/ bug. Now it worries me what else could be breaking. – Inspired_Blue Sep 08 '22 at 11:10
  • Would you know by any chance why at doesn't support seconds? – Inspired_Blue Sep 08 '22 at 11:16
  • 1
    I did not develop it, so no. Likely because in most cases a resolution up to a single minute is plenty for practical purposes. – vanadium Sep 08 '22 at 12:19