1

The shutdown command can only set timer in minutes and hours, but not in seconds and this suprises me.

From man page

The time string may either be in the format "hh:mm" for hour/minutes specifying the time to execute the shutdown at, specified in 24h clock format. Alternatively it may be in the syntax "+m" referring to the specified number of minutes m from now. "now" is an alias for "+0", i.e. for triggering an immediate shutdown. If no time argument is specified, "+1" is implied.

What I try to do is make a button in my window manager (qtile). After pressing this button with my left mouse button I want to shutdown my computer in 5 seconds, but in case I miss-clicked I want click on that button with the right mouse button and cancel this.

sleep 5; shutdown now will not work if I want to be able to cancel.

Is there a way to make cancelling work ? Maybe there is some shutdown-like program with seconds-precission ?

1
  • 2
    Did you try sleep 5 && shutdown now. That way, if you interrupt sleep, it'll return a non-zero exit status and the short-circuit evaluation will not run shutdown. Jul 29, 2020 at 22:57

1 Answer 1

2

Following Andy Dalton's advice (see the comment in your question) is the best way to do this.

But if you really want a program to do this, it's easy to write it:

int main(void) { sleep(5); system("shutdown -h now"); }

If you place this in secondsshutdown.c and compile it with gcc secondsshutdown.c -o secondsshutdown it should do what you want.

(This code can be enhanced so that you no longer have warnings when compiling or to make sure that it accepts options. I didn't do this here so that it becomes easier to read)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .