4

So I have a ubuntu server that has two tmux session that are running in the background and a few other applications on the background. Is there a way to create a scrip that would gracefully exit the process on tmux (excample Cntrl C) and shutdown all the services and reboot?

My machine tends to stop connecting to the internet after a few days and wanted to have the system auto reboot gracefully. I have all the services restart and the mux restart after a reboot.

I just need to figure out how I can get this computer to restart nicely.

Please advise with any info you can to help. Please keep in mind I am a noob but understand the basics.

Thank you in advance.

adil
  • 43

1 Answers1

5

Actually shutdown and its equivalents are graceful. They first send a SIGTERM (15) to the processes and only if they don't respond whithin a certain amount of time they send a SIGKILL (9) afterwards.

SIGTERM is the signal that is sent when you hit CTRL+C. The applications can intercept that signal and shutdown themselves gracefully (e.g. flush files, commit/rollback transactions, etc.). All reasonable applications behave this way.

When the processes don't stop after a while, then a SIGKILL is sent to them afterwards. SIGKILL is kinda sledgehammer. The applications cannot intercept that signal — it's rather like cutting off the power.

So, yes, shutdown or reboot is usually safe and graceful.

Reference:

PerlDuck
  • 13,335
  • So I am not loosing any data but i dont want my server to have any data corruption. Since its running apache and mysql and processing data on the database. So I am hearing that I can use the command /sbin/shutdown -t 15 -r for it to shutdown and kill apps in 15 sec and then reboot. So do i create a script to run this command at a set interval? Or am i way off? – adil Mar 03 '18 at 19:01
  • This would be another question. apache and mysql themselves are definitely aware of a shutdown event and in general handle that properly. But if you have a long running transaction (minutes or so) in your MySQL instance then it will depend on a) your DB engine (e.g. MyISAM vs. InnoDB) and b) on your overall application logic whether the data is consistent after issuing shutdown. It's unlikely that it is corrupted but it may be inconsistent when you don't use proper DB transactions. – PerlDuck Mar 03 '18 at 19:12
  • 1
    On the shutdown, when you give it an option for an amount of time, it doesn't send the sigterm and wait that amount of time, it waits that amount of time before sending the Sig term. The time is more meant to be a delay so that other users on the box can have time to save their work potentially. It was really helpful in multi-user systems to know that a reboot was coming in say 10 minutes. Since that information is sent to all TTYs when the shutdown is initiated. – Rowan Hawkins Mar 04 '18 at 13:45