Hi, I'm very new to C++. I've written a pretty neat chatbot and have her doing some system commands. She'll be running on a battery powered SBC so I'm having her monitor system voltage and execute a little program to do a delayed shut down before she ends her own program when the battery gets critically low.
The problem is she starts the shutdown program and waits for it, lol.
Could someone point me in the right direction? I looked at multithreading but that doesn't seem correct for this application.
Thanks in advance, Tom
What OS if any is this?
Both unix and windows have a 'shutdown in X seconds' command you could leverage. Give yourself 10 seconds or something, then proceed to clean up and exit your code.
Thank you jonnin! That's just what I needed to know, I am indeed running Linux.
I was using std::system ("poweroff"); and I switched to std::system ("shutdown -h +1");
which isn't the few seconds I was looking for, but a minute gets the job done.
Couldn't find a way to drop to seconds, when I tried 00:10 it schedeled the shutdown for ten minutes after midnight.
salem c, Apologies, I should have posted the code, but at the time believed a c++ solution I didn't know would be the ticket so really nothing to show.
Thank you both for helping.
yea it looks like linux is tied to 1 min intervals.
you can write your own 'shutdown now' program, eg
main()
sleep(10000) //10 seconds
system(poweroff)
and call that program from 'system' if you really want to get fired up about it.
Actually that's how this all started, but my program would wait for all of that to execute and a computer would turn off before the program terminated. The idea was that the program could sense low battery voltage and do a clean shutdown After exiting its program.
This is a perfect use case for exec(). It replaces your process with the executed one. So have your program exec() shutdown. Just be sure to add some error checking code after you call exec() in case it doesn't work.
That looks interesting, I'll study exec(). Right now it waits for the threads to join and initiates a shutdown sequence to shut the computer down in a minute. exec() is pretty uncommon, I haven't found a lot of information online about using it so far but if I do exec -h I can see it.