4

software recommendation needed! I'm looking for an alarm clock to use on my Ubuntu 20.04 machine, with a command-line interface to enable me to set alarms from the command line. This is because in my workflow I often have to set many alarms a day, usually only about an hour out. Since I do this so often it's a pain to mess with a GUI all the time.

I tried GNOME clocks of course, but seems it has no command-line interface.

Can anyone recommend anything like what I'm looking for? Not too picky, just need it to do its job.

Thanks in advance :)

Noga S
  • 45

2 Answers2

6

You can use the at command (not installed by default on Ubuntu 20.04). It allows you to execute a command at a specific time. You could for example have a Zenity dialog pop up to serve as an alarm.

vanadium
  • 88,010
6

You can use the sleep command, which comes preinstalled on Ubuntu, to run a command after the specified amount of time as:

sleep NUMBER[SUFFIX] && command

From man sleep:

DESCRIPTION
       Pause for NUMBER seconds.  SUFFIX may be 's' for seconds (the default), 'm'
       for minutes, 'h' for hours or 'd' for days.  Unlike most implementations
       that require NUMBER be an integer, here NUMBER may be an arbitrary floating
       point number.  Given two or more arguments, pause for the amount of time
       specified by the sum of their values.

For example, you may run:

sleep 3h 5m 25s && mpv file

to open file with the mpv media player after 3 hours, 5 minutes and 25 seconds.


sleep is perfectly usable and straightforward for running commands after a certain amount of time, however at is better for running a command at (no surprise here) a specified time.

Nevertheless, a similar behavior can be implemented with sleep. Here is an example script:

#!/bin/bash
alarm_time=$1
command=$2

convert alarm_time and current_time to seconds since 1970-01-01 00:00:00 UTC

and calculate their difference

alarm_time=$(date -d "$alarm_time" +"%s") current_time=$(date +"%s") diff=$(( $alarm_time - $current_time ))

convert the time difference to hours

sleep_time=$(echo "scale=5; $diff/3600" | bc)

run the command at the specified time

sleep "$sleep_time"h && $command

After saving it as alarm.sh (choose your preferred name) and making it executable, you can run it as:

/path/to/alarm.sh time command

You can add the script to your path for easier access, if you wish, and you can also hard code the command to run by replacing $2 with the command you want to run.

  • 1
    Difficult to have that fire at 15:00 sharply, though. – vanadium Aug 31 '20 at 07:06
  • 1
    @vanadium Agree. The advantage of sleep is that it is preinstalled and is perfectly usable and straightforward for running commands after a certain amount of time. To run a command at a specified time, it is pretty easy to implement a script that uses sleep. Of course it would be a waste of time, since at already does that. – BeastOfCaerbannog Aug 31 '20 at 07:19
  • Interesting, but that is not reflected in your answer. If you could elaborate how you can use sleep to do something at a specified time, then it would be OK. b.t.w. at is a pretty standard GNU utility that happens not to be installed anymore in current Ubuntu versions. – vanadium Aug 31 '20 at 08:59
  • @vanadium I added an example script to the answer. Please check it out. – BeastOfCaerbannog Aug 31 '20 at 10:52
  • Indeed, calculating the time it takes to reach the target time is a trick. So one can survive with Ubuntu on an island where installing at is not an option :-) – vanadium Aug 31 '20 at 12:47
  • I know this may sound like a new question, but related to this: is there an easy way to play a system sound as alarm sound instead of having to open a media player and play an .mp3 or similar file? – Andyc Dec 06 '21 at 20:05
  • @Andyc This should really be a new question, so please ask one. You can refer to this thread to provide context if needed. Thanks! – BeastOfCaerbannog Dec 07 '21 at 08:40