Aaron
Aaron

Reputation: 113

Is there a way to suspend OS shutdown or restart in C++?

I'm planning to develop a cross-platform application that may need to do processing prior to the user's Operating System shutting down or restarting. Is there a way to temporarily suspend, or override, an OS shutdown/restart in C++?

As an aside, this feature will be entirely the user's choice via configuration and disabled by default.

I feel this might not be possible without being invasive to the Operating System, but I thought I'd ask for the community's thoughts.

If my only option is to somewhat "hack" the OS in order to achieve this feature, then I'll not pursue this idea any further. :)

Thanks in advance!

Upvotes: 2

Views: 440

Answers (5)

Aaron
Aaron

Reputation: 113

Thanks to all responses and ideas!

Judging by the responses received, it seems there is no "safe" and cross-platform way of achieving such functionality easily using C++.

Pete Becker mentioned there could be specific rules per Operating System. This makes sense, and these rules are also always subject to change.

Others have suggested using the system() command. However, this would potentially introduce further issues and vulnerabilities (1) (2) into the system (there may be better explanations and/or examples, so please edit this post if/when you find them).

Considering all of the above, my suggestion would be to configure the application to either have the user manually execute these processes via a GUI (leaving it entirely up to them), execute these processes on startup of the Operating System or both.

Thanks again to all who responded!

Upvotes: 0

Kwang-Chun Kang
Kwang-Chun Kang

Reputation: 351

I have googled the related article that how to detect Linux shutdown/reboot But it seems hard to implement for your purpose.

What comes from my mind is; if you can add your program as a service, the linux system will closing the service in advance of its shutdown.

Good Luck

Upvotes: 0

Rama
Rama

Reputation: 3305

To override a scheduled/in progress shutdown you can:

For Windows 7

system("shutdown /a");

For Linux

system("shutdown -c");

Upvotes: 0

R Sahu
R Sahu

Reputation: 206717

Is there a way to temporarily suspend, or override, an OS shutdown/restart in C++?

There is no such functionality in standard C++.

Unless there is a platform-specific API to do that, I am afraid you won't be able suspend or override an OS shutdown/restart operation in C++.

Upvotes: 3

Pete Becker
Pete Becker

Reputation: 76498

There's nothing in C++ for this. But a typical GUI (Windows, Mac, etc.) will notify your application of a pending shutdown so that you can do final processing. That's specific to the OS, and subject to its rules.

Upvotes: 2

Related Questions