reboot computer, but save all data

I am relatively new to linux and I was trying to write a program that reboots the computer.
I tried using system("reboot") and also with shutdown or init, but they all just terminate immediately.

I am looking for a command that reboots the computer but first stops all programs.
When I am working in e.g. gedit and the file was not saved yet then I expect the question whether I would like to save it.

All ways I have tried so far rebooted immediately and all data is lost.
I think that shutdown has options to specify a grace period. Try man shutdown.

Thanks for your reply, but I already found out that you can order a shutdown with a time parameter.

If I have an own written program I expect that when the sytem goes down the close event is send to the window and that it has a few seconds to do what it has to do before closing.

Running shutdown with a time parameter other than 'now' will only give you some minutes to close the window yourself, but I was hoping to find a way to do this automatically.
On Solaris at least, you can optionally send a message warning users that the system is about to come down when you use shutdown. On Linux, I'm not sure but I'm sure you could write a script to do it if not.
That won't help much because I am the only user on the computer (I should have said that earlier).

I had an idea of sending some SIGTERM messages manually before I rebooted, but I noticed that my program responds differntly than expected.
My program has an endless loop (to test what would happen), but when it receives the message to terminate it just quits. So even without the loop it would still just end without being able to close everything properly.
This means that the problem was not be with the shutdown command, but with the way my program reacts on it.

I found a way to trap the terminate signal with "signal(SIGTERM, Cleanup);".
With doing this the function Cleanup is called wich will then clean up and exit the program.

Thanks for your help anyway.
Topic archived. No new replies allowed.