• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

initiatesystemshudown equivalent in linux

In Windows, the function InitiateSystemShutdown  can shutdown or reboot the system. In linux, we can achive this with shutdown or reboot command running as root or sudo.
    However, when we pass such commands in linux, all the processes stop working.

   I am currently porting my source code from windows to linux. and InitiateSystemShutdown  system call is written an API, which is called by some modules.
      InitiateSystemShutdown  system calls returns status to caller and caller can take the action on the same.
    In linux, if shutdown or reboot commands fails, then my caller will get the status.
  1)  How I can pass time parameter in shutdown or linux command. The parameter required in seconds. e.g. 5 seconds.
  2) Is there any check to know whether I have authority to shutdown / reboot the system. So that based on that authority I can return some status to caller
The parameter passed to InitiateSystemShutdown  functions are
BOOL WINAPI InitiateSystemShutdown(
  __in_opt  LPTSTR lpMachineName,
  __in_opt  LPTSTR lpMessage,
  __in      DWORD dwTimeout,
  __in      BOOL bForceAppsClosed,
  __in      BOOL bRebootAfterShutdown
lpMachineName [in, optional] 
The network name of the computer to be shut down. If lpMachineName is NULL or an empty string, the function shuts down the local computer.
lpMessage [in, optional] 
The message to be displayed in the shutdown dialog box. This parameter can be NULL if no message is required. 
dwTimeout [in] 
The length of time that the shutdown dialog box should be displayed, in seconds. While this dialog box is displayed, the shutdown can be stopped by the AbortSystemShutdown function. 
If dwTimeout is not zero, InitiateSystemShutdown displays a dialog box on the specified computer. The dialog box displays the name of the user who called the function, displays the message specified by the lpMessage parameter, and prompts the user to log off. The dialog box beeps when it is created and remains on top of other windows in the system. The dialog box can be moved but not closed. A timer counts down the remaining time before a forced shutdown.
If dwTimeout is zero, the computer shuts down without displaying the dialog box, and the shutdown cannot be stopped by AbortSystemShutdown.
bForceAppsClosed [in] 
If this parameter is TRUE, applications with unsaved changes are to be forcibly closed. Note that this can result in data loss.
If this parameter is FALSE, the system displays a dialog box instructing the user to close the applications.
bRebootAfterShutdown [in] 
If this parameter is TRUE, the computer is to restart immediately after shutting down. If this parameter is FALSE, the system flushes all caches to disk and safely powers down the system.
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Open in new window

1 Solution
shutdown t 5  r your computer will shutdown in 5 seconds
This command will cause the system to reboot in 5 seconds. Change the 5 to another value in seconds if desired.
shutdown -t 5 -r your computer will restart in 5 seconds

This command will cause the system to reboot in 5 seconds. Change the 5 to another value in seconds if desired.

(Sorry Word Hacked up my dashes)

shutdown -t 5 -r -k your computer will restart in 5 seconds

This command will complete if you have privileges, but will not actually initiate the restart. This command will only display the message to your terminal. You can also redirect your output if you want to use it in a script.
man shutdown gives you loads of info about the options...

or you could;
   sleep <number of seconds here>
   shutdown -r 0 (reboot)
   shutdown -h 0

I occasionally use sleep in scripts because it's easier to break out.
evilrixSenior Software Engineer (Avast)Commented:

The reboot() call reboots the system, or enables/disables the reboot keystroke (abbreviated CAD, since the default is Ctrl-Alt-Delete; it can be changed using loadkeys(1)).

This system call will fail (with EINVAL) unless magic equals LINUX_REBOOT_MAGIC1 (that is, 0xfee1dead) and magic2 equals LINUX_REBOOT_MAGIC2 (that is, 672274793). However, since 2.1.17 also LINUX_REBOOT_MAGIC2A (that is, 85072278) and since 2.1.97 also LINUX_REBOOT_MAGIC2B (that is, 369367448) and since 2.5.71 also LINUX_REBOOT_MAGIC2C (that is, 537993216) are permitted as value for magic2. (The hexadecimal values of these constants are meaningful.) The flag argument can have the following values:

    (RB_AUTOBOOT, 0x1234567). The message 'Restarting system.' is printed, and a default restart is performed immediately. If not preceded by a sync(2), data will be lost.
    (RB_HALT_SYSTEM, 0xcdef0123; since 1.1.76). The message 'System halted.' is printed, and the system is halted. Control is given to the ROM monitor, if there is one. If not preceded by a sync(2), data will be lost.
    (0x4321fedc; since 2.1.30). The message 'Power down.' is printed, the system is stopped, and all power is removed from the system, if possible. If not preceded by a sync(2), data will be lost.
    (0xa1b2c3d4; since 2.1.30). The message 'Restarting system with command '%s'' is printed, and a restart (using the command string given in arg) is performed immediately. If not preceded by a sync(2), data will be lost.
    (RB_ENABLE_CAD, 0x89abcdef). CAD is enabled. This means that the CAD keystroke will immediately cause the action associated with LINUX_REBOOT_CMD_RESTART.
    (RB_DISABLE_CAD, 0). CAD is disabled. This means that the CAD keystroke will cause a SIGINT signal to be sent to init (process 1), whereupon this process may decide upon a proper action (maybe: kill all processes, sync, reboot).

Only the superuser may use this function.
int shut(int delay int action)
   return reboot(action);
int main(void)
    int i=0;   
    int retval = shut(10, LINUX_REBOOT_CMD_RESTART);     
    // Handle if retval is error state

Open in new window

Note that your application needs the  CAP_SYS_BOOT capability for reboot() to succeed.  See man capabilities(7).  You can check what capabilities are available to your application before it calls reboot() using capget() or cap_get_proc().
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now