i would like some examples of simple c or c++ codes that can shut down a computer like say at exactly 9pm.

could anyone give me a sample c code that can pop up on the screen warning that the computer will shut down in 20 minutes.

Dani AI

Generated

asked for C/C++ ways to show a 20‑minute warning and shut a machine at 9:00. and correctly flagged that platform and privileges matter. Below are compact, practical options you can apply on modern Windows and Unix-like systems, plus quick safety and permission checks.

On Windows the simplest, robust method is two scheduled actions: one that displays a warning program (MessageBox or a small GUI) 20 minutes before, and one that performs the shutdown at 21:00. Example task-creation commands (run elevated):

schtasks /Create /SC ONCE /TN "WarnShutdown" /TR "C:\Tools\Warn.exe" /ST 20:40
schtasks /Create /SC ONCE /TN "DoShutdown" /TR "C:\Windows\System32\shutdown.exe /s /t 0" /ST 21:00

A tiny C++ warning program can just call MessageBox. To perform a programmatic shutdown you either call the system API (requires the shutdown privilege) or invoke shutdown.exe from your process.

On Linux/Unix, schedule the warning and the shutdown with cron or a systemd timer. Broadcast a text warning to logged-in users with wall (desktop notifications need a DISPLAY/DBUS environment from the user session). Example cron lines (root):

40 20 /usr/bin/wall "System will shut down in 20 minutes"
0 21
/sbin/shutdown -h now

Programs that call reboot/shutdown directly must run as root or have CAP_SYS_BOOT capability.

Safety and troubleshooting tips: always test in a VM first; ensure open files/apps are saved before forcing poweroff; consider an abort path (Windows: abort a timed shutdown with shutdown /a while it is pending); check elevation in code (Linux: check geteuid()==0; Windows: use token checks). Account for time zones and daylight‑saving changes if you schedule recurring tasks.

Recommended Answers

All 2 Replies

This is the part where we sit around guessing which OS/Compiler you have, and whether or not you have admin privileges on the machine in question.

Some windows, and all Unix/Linux operating systems have the concept of 'shutdown' to take a machine down in an orderly fashion, and the concept of 'at' for arranging things to happen at predetermined times in the future. Perhaps play with these to achieve what you want.

In unix use cronjob, in windows create a service and call shutdown
I hope you are not asking us to code your homework.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.