Recent

Author Topic: Linux Shutdown revisited  (Read 9307 times)

KarenT

  • Full Member
  • ***
  • Posts: 120
Linux Shutdown revisited
« on: July 25, 2017, 04:55:59 pm »
Hello,

With thanks to Member taazz, this code works for shutting down Ubuntu.
Code: Pascal  [Select][+][-]
  1.    fpSystem('sudo /sbin/shutdown -P now');
  2.  

But, I find it prevents some of my program Close chores, so I did some reading on Shutdown and changed the code to allow my Close stuff to get done. But the call fpSystem" never returns to to do any further processing. The help says "waits for the command to complete" That might be good normally, but not for a shutdown.

How can I start the shutdown process, but still close my program in an orderly fashion?

Code: Pascal  [Select][+][-]
  1.    fpSystem('sudo /sbin/shutdown -P +1'); // This works and shuts Ubuntu down
  2.    Application.ProcessMessages;  <-- didn't work
  3.   Application.Terminate; <-- didn't work
  4.     // or
  5.     Close; <-- didn't work
  6.    // or
  7.    Halt; <-- didn't work
  8.  

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: Linux Shutdown revisited
« Reply #1 on: July 25, 2017, 05:21:12 pm »
something like this:
Code: Pascal  [Select][+][-]
  1. uses
  2.   Process;
  3.  
  4. var
  5.   AProcess: TProcess;
  6. begin
  7.   AProcess := TProcess.Create(nil);
  8.   AProcess.Options := AProcess.Options - [poWaitOnExit];
  9.   AProcess.Executable := '/bin/sh';
  10.   AProcess.Parameters.Add('-c');
  11.   AProcess.Parameters.Add('sudo /sbin/shutdown -P now');
  12.   AProcess.Execute;
  13.   //do something else
  14. end;
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Linux Shutdown revisited
« Reply #2 on: July 25, 2017, 05:48:09 pm »
Code: Pascal  [Select][+][-]
  1.    fpSystem('sudo /sbin/shutdown -P +1'); // This works and shuts Ubuntu down
  2.    Application.ProcessMessages;  <-- didn't work || Grrr...OF COURSE NOT THE KERNEL IS IN SHUTDOWN STATE. PLZ DON'T BE STUPID...
  3.  
KarenT,

Where is the logic?
You just asked the kernel to shutdown.
That means there is *no way* it will allow to process any user messages.......In USER SPACE!
It also means it is pretty safe. The kernel will resolve any memory issues including the swap.
But.... where is the logic? Think!... You put the system in shutdown state and then ask for more?
Think again.......
« Last Edit: July 25, 2017, 05:52:06 pm by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Linux Shutdown revisited
« Reply #3 on: July 25, 2017, 06:00:31 pm »
Code: Pascal  [Select][+][-]
  1. u  AProcess.Options := AProcess.Options - [poWaitOnExit];
  2.  
Just does not wait for user space. It waits on kernel processes and will NOT process further user space messages that do not communicate with the kernel..
But your answer and processing order is a lot better.
« Last Edit: July 25, 2017, 06:02:59 pm by Thaddy »
Specialize a type, not a var.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Linux Shutdown revisited
« Reply #4 on: July 25, 2017, 06:14:05 pm »
Hello,

With thanks to Member taazz, this code works for shutting down Ubuntu.
Code: Pascal  [Select][+][-]
  1.    fpSystem('sudo /sbin/shutdown -P now');
  2.  

But, I find it prevents some of my program Close chores, so I did some reading on Shutdown and changed the code to allow my Close stuff to get done. But the call fpSystem" never returns to to do any further processing. The help says "waits for the command to complete" That might be good normally, but not for a shutdown.

How can I start the shutdown process, but still close my program in an orderly fashion?

Code: Pascal  [Select][+][-]
  1.    fpSystem('sudo /sbin/shutdown -P +1'); // This works and shuts Ubuntu down
  2.    Application.ProcessMessages;  <-- didn't work
  3.   Application.Terminate; <-- didn't work
  4.     // or
  5.     Close; <-- didn't work
  6.    // or
  7.    Halt; <-- didn't work
  8.  
Code: Pascal  [Select][+][-]
  1. var //global somewhere
  2.   Shutdown : boolean = false; // probably you need to use the units initialization section to set the false.
  3. ....
  4.    shutdown := true; //fpSystem('sudo /sbin/shutdown -P now');
  5.    application.terminate;
  6.  
and then in the projects lpr file;
Code: Pascal  [Select][+][-]
  1.   RequireDerivedFormResource:=True;
  2.   Application.Initialize;
  3.   Application.CreateForm(TForm1, Form1);
  4.   Application.Run;
  5.   if Shutdown then fpSystem('sudo /sbin/shutdown -P now');
  6.  
done you have properly exit your application and shutdown the system.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Linux Shutdown revisited
« Reply #5 on: July 25, 2017, 06:37:35 pm »
Taaz that's about the correct order..
Specialize a type, not a var.

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: Linux Shutdown revisited
« Reply #6 on: July 25, 2017, 07:31:06 pm »
The trouble with all of these solutions is that they will not work unless sudo is installed and configured in the appropriate way. Many distros leave this as a choice for the installer to make, and many users prefer not to use sudo.

KarenT

  • Full Member
  • ***
  • Posts: 120
Re: Linux Shutdown revisited
« Reply #7 on: July 25, 2017, 08:32:43 pm »
Grrr...OF COURSE NOT THE KERNEL IS IN SHUTDOWN STATE. PLZ DON'T BE STUPID...

Steady on with the aggression, and invective. By putting "PLZ" ( I assume you meant "PLEASE") before "STUPID" does not soften the statement. Shame.

As I saw it, by using the "+1" the system will shut down in one minute's time. As such HAD the thing returned as I believe it should, it WOULD process the remainder of the code and be ready for the impending shutdown.

IF I use the Terminal and issue that shutdown command with the "+1" it notifies all users that the system is about to shut down in one minute. During that one minute, I am free to do anything I like within Ubuntu -- INCLUDING abort the shutdown.

So, it is not s great stretch to expect Lazarus to work similarly. I believe this is an oversight by the developers that the fpSystem call does not try to differentiate between commands that need a "wait" and those that do not.

To my mind, fpSystem should be
Code: Pascal  [Select][+][-]
  1.   fpSystem(Cmd : String; Wait : Boolean = True);
  2.  

p.s.
If I called you STUPID (I am not) for failing to think it through, do you supposed the Mods would censure me because I am a relative newbie and not a seasoned commenter?

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Linux Shutdown revisited
« Reply #8 on: July 25, 2017, 08:54:40 pm »
True is for PENDING calls, not for NEW calls after you asked the system to shut down... Seriously.... < seriously grumpy  >:D >:D >:D>
If you think you know better, plz be my guest. You are fired. < Playing "The Donald">
Noobs should learn to pay attention to clowns like me with 30+ years of experience. The really experienced ones do... It is always the noobs that don't listen. But I do make mistakes. Not in this case....
Btw: the moderators won't interfere... They know I am not half way offensive..... O:-)

Think of it as a bar: doors closed at 2:00, customers can still pay, finish their drink and leave, new customers not allowed in. You are playing new customer after the doors closed.
« Last Edit: July 25, 2017, 09:12:00 pm by Thaddy »
Specialize a type, not a var.

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Linux Shutdown revisited
« Reply #9 on: July 25, 2017, 09:27:02 pm »
Grrr...OF COURSE NOT THE KERNEL IS IN SHUTDOWN STATE. PLZ DON'T BE STUPID...

Steady on with the aggression, and invective. By putting "PLZ" ( I assume you meant "PLEASE") before "STUPID" does not soften the statement. Shame.

Hey Karen, by and large, the experts on here have a winning combination of knowledge and humility and are very friendly and helpful. There are, however, a couple of individuals who, while scoring highly on the knowledge side of the equation, fall behind a few notches on basic people skills. Trouble is that if you point it out to them, you tend to get a veritable shitstorm of unrepentant bleating born of injured pride in return. The best response, I think, despite the obvious temptation to verbally rip them a new one, is not to respond at all.

Cheers,
Carl
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Linux Shutdown revisited
« Reply #10 on: July 25, 2017, 09:31:54 pm »
Carl.
Think of it as a bar: doors closed at 2:00, customers can still pay, finish their drink and leave, new customers not allowed in. You are playing new customer after the doors closed. :-*

How else would you explain it?
« Last Edit: July 25, 2017, 09:36:21 pm by Thaddy »
Specialize a type, not a var.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Linux Shutdown revisited
« Reply #11 on: July 25, 2017, 09:34:01 pm »
I like the pseudo BARcode...  :D
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Linux Shutdown revisited
« Reply #12 on: July 25, 2017, 10:02:19 pm »
Carl.
Think of it as a bar: doors closed at 2:00, customers can still pay, finish their drink and leave, new customers not allowed in. You are playing new customer after the doors closed. :-*

How else would you explain it?

Well, one or both of us has had far too many pints this evening. I really didn't understand that at all. Sir, you have out-metaphored me! My congratulations to you!
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Linux Shutdown revisited
« Reply #13 on: July 25, 2017, 10:08:39 pm »
Carl.
Think of it as a bar: doors closed at 2:00, customers can still pay, finish their drink and leave, new customers not allowed in. You are playing new customer after the doors closed. :-*

How else would you explain it?

Well, one or both of us has had far too many pints this evening. I really didn't understand that at all. Sir, you have out-metaphored me! My congratulations to you!

And besides, I thought this joint was licensed for 24hr/day... 😉
« Last Edit: July 25, 2017, 10:13:15 pm by carl_caulkett »
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Linux Shutdown revisited
« Reply #14 on: July 25, 2017, 10:24:45 pm »
Anyway, I hope KarenT get's it. And yes this joint seems to be open 24/7.
Specialize a type, not a var.

 

TinyPortal © 2005-2018