标签: shutdown

使用Java关闭Windows

是否可以使用Java以编程方式关闭Windows?

干杯

java windows shutdown

9
推荐指数
1
解决办法
2万
查看次数

当关机挂钩坏了

如果我在我的Java程序的运行时添加一个关闭钩子,如下所示:

public class MyShutdownHook implements Runnable
{
    @Override
    public void run()
    {
        // Stuff I want executed anytime
        // the program, Java, or the OS exits normally,
        // crashes, or terminates unexpectedly for any reason.
    }
}

// The in another method...
Runtime.getRuntime().addShutdownHook(new MyShutdownHook());
Run Code Online (Sandbox Code Playgroud)

......那么有没有曾经在那里,任何情况下,run()方法将不会执行该程序/ Java的/ OS退出时正常,崩溃或意外终止?如果是这样,什么情况下能够绕过Runtime关闭钩子,为什么?

java runtime shutdown

9
推荐指数
1
解决办法
2382
查看次数

停止和关机

Halt和Poweroff命令有什么区别?据我所知,他们两个都导致机器关机,那为什么2个命令呢?

我想知道这个,因为在我的虚拟机中.Poweroff导致VM完全关闭.Whrereas停止导致杀死所有进程,然后导致100%CPU利用率的停顿.在给出msg"系统将要关闭"之后.当我使用暂停来关闭我的电脑时,这不会发生.基本上,停止和断电都会导致独立计算机(无VM)正常关闭.

linux shell shutdown

9
推荐指数
1
解决办法
1万
查看次数

当我无法访问CLI工具时,如何关闭JBoss Wildfly?

我使用Wildfly 10.0.0.CR2和Java 8.我让Wildfly在端口8080上侦听http连接,并且过去使用此命令关闭服务器...

./jboss-cli.sh --connect command=:shutdown
Run Code Online (Sandbox Code Playgroud)

HOwever,偶尔,我无法访问此工具,即使服务器仍在运行.请注意我的Mac下面的交互...

Daves-MacBook-Pro-2:bin davea$ ./jboss-cli.sh --connect command=:shutdown
Failed to connect to the controller: The controller is not available at localhost:9990: java.net.ConnectException: WFLYPRT0023: Could not connect to http-remoting://localhost:9990. The connection timed out: WFLYPRT0023: Could not connect to http-remoting://localhost:9990. The connection timed out
Daves-MacBook-Pro-2:bin davea$ telnet localhost 8080
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Run Code Online (Sandbox Code Playgroud)

我的问题是,什么是关闭JBoss服务器的万无一失的方法?注意我更喜欢不依赖于CLI工具的方法.

jboss shutdown wildfly

9
推荐指数
1
解决办法
2万
查看次数

如何正确关闭java ExecutorService

我有一个简单的java ExecutorService运行一些任务对象(实现Callable).

ExecutorService exec = Executors.newSingleThreadExecutor();
List<CallableTask> tasks = new ArrayList<>();
// ... create some tasks
for (CallableTask task : tasks) {
 Future future = exec.submit(task);
 result = (String) future.get(timeout, TimeUnit.SECONDS);
 // TASKS load some classes and invoke their methods (they may create additional threads)
 // ... catch interruptions and timeouts
}
exec.shutdownNow();
Run Code Online (Sandbox Code Playgroud)

在完成所有任务(DONE或TIMEOUT-ed)之后,我尝试关闭执行程序,但它不会停止:exec.isTerminated() = FALSE. 我怀疑某些被执行的任务未正确终止.

是的,我知道执行者的关闭不能保证任何事情:

除尽力尝试停止处理主动执行任务之外,没有任何保证.例如,典型的实现将通过{@link Thread#interrupt}取消,因此任何未能响应中断的任务都可能永远不会终止.

我的问题是,有没有办法确保这些(任务)线程终止?我提出的最佳解决方案是System.exit()在程序结束时调用,但这很简单.

java multithreading shutdown executorservice

9
推荐指数
2
解决办法
8073
查看次数

在Mac OSX上接收电源通知(尤其是关机)

我正在用C编写一个用于Mac(Leopard)的应用程序,它需要在接收电源通知时做一些工作,例如睡眠,唤醒,关机,重启.它launchd在登录时作为启动运行,然后开始监视通知.我用来做这个的代码如下:

/* ask for power notifications */
static void StartPowerNotification(void)
{
    static io_connect_t rootPort;   
    IONotificationPortRef notificationPort;
    io_object_t notifier;

    rootPort = IORegisterForSystemPower(&rootPort, &notificationPort, 
                                        PowerCallback, &notifier);
    if (!rootPort) 
        exit (1);

    CFRunLoopAddSource (CFRunLoopGetCurrent(),  
                        IONotificationPortGetRunLoopSource(notificationPort), 
                        kCFRunLoopDefaultMode);
}

/* perform actions on receipt of power notifications */
void PowerCallback (void *rootPort, io_service_t y, 
                    natural_t msgType, void *msgArgument)
{
    switch (msgType) 
    {
        case kIOMessageSystemWillSleep:
            /* perform sleep actions */
            break;

        case kIOMessageSystemHasPoweredOn:
            /* perform wakeup actions */
            break;

        case kIOMessageSystemWillRestart:
            /* perform restart actions …
Run Code Online (Sandbox Code Playgroud)

c macos shutdown

8
推荐指数
1
解决办法
7063
查看次数

如何知道当前是否正在关机?

在Tomcat中,当服务器关闭时,它会尝试将它知道的类中的所有静态变量设置为null.通过触摸这些类,它们的静态初始化器运行,这可能导致我们的一些具有大量静态初始化器的遗留类的无限循环.

是否有一种优雅的方法来检测当前是否正在关闭?然后我们可以在静态初始化器的顶部检查我们是否处于关闭模式,然后忽略初始化.

我们找到的唯一可行的方法就是优雅:

    try{
       Thread hook = new Thread();
       Runtime.getRuntime().addShutdownHook( hook ); // fires "java.lang.IllegalStateException: Shutdown in progress" if currently in shutdown
       Runtime.getRuntime().removeShutdownHook( hook );
    }catch(Throwable th){
       throw new Error("Init in shutdown thread", th );
    }
Run Code Online (Sandbox Code Playgroud)

java tomcat shutdown

8
推荐指数
1
解决办法
1989
查看次数

如何永久保持python HTTP Server?

我在python中编写了一个简单的HTTP服务器,通过Web UI管理服务器上托管的数据库.它功能完善,按预期工作.然而,它有一个巨大的问题,它不会留下来.它会工作一个小时左右,但如果在返回使用它时长时间不使用,我每次都必须重新初始化它.现在我使用的方法是:

def main():
    global db
    db = DB("localhost")
    server = HTTPServer(('', 8080), MyHandler)
    print 'started httpserver...'
    server.serve_forever()

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

我在linux服务器的后台运行这个,所以我会运行像sudo python webserver.py这样的命令来分离它,但正如我之前提到的那样,它会退出.任何建议都是值得赞赏的原因,因为它看起来我不明白为什么它关闭.

python shutdown http

8
推荐指数
1
解决办法
5228
查看次数

pthread_key_create析构函数没有被调用

根据pthread_key_create手册页,我们可以关联在线程关闭时调用的析构函数.我的问题是我注册的析构函数没有被调用.我的代码的要点如下.

static pthread_key_t key;
static pthread_once_t tls_init_flag = PTHREAD_ONCE_INIT;

void destructor(void *t) {
  // thread local data structure clean up code here, which is not getting called
}

void create_key() {
  pthread_key_create(&key, destructor);
}

// This will be called from every thread
void set_thread_specific() {

  ts = new ts_stack; // Thread local data structure

  pthread_once(&tls_init_flag, create_key);
  pthread_setspecific(key, ts);
}
Run Code Online (Sandbox Code Playgroud)

知道什么可能会阻止这个析构函数被调用?我现在也在使用atexit()来在主线程中进行一些清理.有没有机会干扰被调用的析构函数?我也尝试删除它.虽然仍然没有奏效.另外我不清楚我是否应该将主线程作为atexit的单独案例来处理.(顺便说一下,使用atexit是必须的,因为我需要在应用程序出口处进行一些特定于应用程序的清理)

c++ shutdown pthreads thread-specific-storage

8
推荐指数
1
解决办法
1669
查看次数

如何在不调用"system()"的情况下使用C++或Qt关闭Linux?

我想在UI上按下关闭按钮时关闭嵌入式Linux.我知道我可以打电话给system:

system("shutdown -P now");
Run Code Online (Sandbox Code Playgroud)

参考:链接

但是知道system不建议使用,我想知道C++中是否还有另一种方法可以做到这一点(如果还有一种使用Qt的特定方法,我也想知道它虽然是一般的C++方法更重要).

c c++ linux qt shutdown

8
推荐指数
2
解决办法
2万
查看次数