使用程式執行關機、重開機、登出指令

使用程式執行關機、重開機、登出指令

安裝一些程式或更新程式後會要求你重新開機,這個動作是如何利用程式達到呢?目前已知的是在Console上面可以下指令。

  • 重開機
sudo shutdown -r now

  • 關機
sudo shutdown -t now

但這要root權限有麻煩煩,當然在OSX下還能使用程式傳送事件的方式告訴系統你要的動作是重開機(Restart)、關機(Shutdown),這些之外還能告訴系統要你要登出(Logout)。

要執行這個功能過程有點覆雜,所以已經將功能整理成Function:SendAppleEventToSystemProcess

//-----------start-----------
OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend)
{
    AEAddressDesc targetDesc;
    static const ProcessSerialNumber kPSNOfSystemProcess = { 0, kSystemProcess };
    AppleEvent eventReply = {typeNull, NULL};
    AppleEvent appleEventToSend = {typeNull, NULL};

    OSStatus error = noErr;

    error = AECreateDesc(typeProcessSerialNumber, &kPSNOfSystemProcess,
                         sizeof(kPSNOfSystemProcess), &targetDesc);

    if (error != noErr)
    {
        return(error);
    }

    error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc,
                               kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend);

    AEDisposeDesc(&targetDesc);
    if (error != noErr)
    {
        return(error);
    }

    error = AESend(&appleEventToSend, &eventReply, kAENoReply,
                   kAENormalPriority, kAEDefaultTimeout, NULL, NULL);

    AEDisposeDesc(&appleEventToSend);
    if (error != noErr)
    {
        return(error);
    }

    AEDisposeDesc(&eventReply);

    return(error);
}

//------------end------------

目前能傳送的AEEventID

  • kAERestart 重開機
  • kAEShutDown 關機
  • kAEReallyLogOut 登出
  • kAESleep 睡眠
  • kAEShowShutdownDialog 顯示是否要關機的對話框
  • kAEShowRestartDialog 顯示是否要重開機的對話框

程式範例提供能在Console下面測試的程式:

//-----------start-----------
#include <stdio.h>
#include <CoreServices/CoreServices.h>
#include <Carbon/Carbon.h>

static OSStatus SendAppleEventToSystemProcess(AEEventID EventToSend);

int main(void)
{
    const int bufferSize = 256;
    OSStatus error = noErr;
    char select [bufferSize];

    printf("1: Restart computer\n");
    printf("2: Shutdown computer\n");
    printf("3: Logout computer\n");
    printf("4: Sleep computer\n");
    printf("5: ShowShutdownDialog\n");
    printf("6: ShowRestartDialog\n");
    printf("q: quit program\n");

    printf("please enter choice:\n");fflush(stdout);
    fgets(select, bufferSize, stdin);

    switch (select[0])
    {
        case '1':
            //sending restart event to system
            error = SendAppleEventToSystemProcess(kAERestart);
            if (error == noErr)
            {printf("Computer is going to restart!\n");}
            else
            {printf("Computer wouldn't restart\n");}
            break;
        case '2':
            //sending shutdown event to system
            error = SendAppleEventToSystemProcess(kAEShutDown);
            if (error == noErr)
            {printf("Computer is going to shutdown!\n");}
            else
            {printf("Computer wouldn't shutdown\n");}
            break;
        case '3':
            //sending logout event to system
            error = SendAppleEventToSystemProcess(kAEReallyLogOut);
            if (error == noErr)
            {printf("Computer is going to logout!\n");}
            else
            {printf("Computer wouldn't logout");}
            break;
        case '4':
            //sending sleep event to system
            error = SendAppleEventToSystemProcess(kAESleep);
            if (error == noErr)
            {printf("Computer is going to sleep!\n");}
            else
            {printf("Computer wouldn't sleep");}
        case '5':
            //kAEShowShutdownDialog
            error = SendAppleEventToSystemProcess(kAEShowShutdownDialog);
            if (error == noErr)
            {printf("Computer is going to ShowShutdownDialog!\n");}
            else
            {printf("Computer wouldn't ShowShutdownDialog");}
        case '6':
            //kAEShowShutdownDialog
            error = SendAppleEventToSystemProcess(kAEShowRestartDialog);
            if (error == noErr)
            {printf("Computer is going to ShowRestartDialog!\n");}
            else
            {printf("Computer wouldn't ShowRestartDialog");}
    };
    return(0);
}
//------------end------------

執行畫面會列出選項讓你選擇要執行哪個功能:

1: Restart computer
2: Shutdown computer
3: Logout computer
4: Sleep computer
5: ShowShutdownDialog
q: quit program
please enter choice:
5
Computer is going to ShowShutdownDialog!
logout


上面例子我執行了選項5後會告知我選擇了ShowShutdownDialog,意指要顯示關機的對話框詢問是否要關機的訊息:

以上就是使用程式執行關機、重開機、登出指令。

參考資料

Programmatically causing restart, shutdown and/or logout