使用程式执行关机、重开机、登出指令

使用程式执行关机、重开机、登出指令

安装一些程式或更新程式后会要求你重新开机,这个动作是如何利用程式达到呢?目前已知的是在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