通过启动在关机时运行Shell脚本 [英] Running a Shell Script on Shutdown via launchd

查看:276
本文介绍了通过启动在关机时运行Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于在OS X上不赞成使用启动项和rc命令(并且在许多情况下根本不起作用),而推荐使用launchd,因此我想找出正确的方法来设置在注销/关闭时运行的Shell脚本.

Since startup items and rc commands are both deprecated (and in many cases not working at all) on OS X in favour of launchd, I'd like to find out what the correct way would be to setup a shell script that runs on logout/shutdown.

对于启动项,可以创建一个类似于以下内容的shell脚本:

For a startup item it was possible to create a shell script that looked something like:

#!/bin/sh
StartService() {
    echo "Started."
}

StopService() {
    echo "Stopped."
}

RunService "$1"

但是从launchd运行脚本时不支持RunService命令,而且我不确定无论如何都不再使用它.

But the RunService command isn't supported when running a script from launchd, and I'm not sure it's meant to be used anymore anyway.

如果可能的话,我想创建一个shell脚本,该脚本将作为按需服务提供给launchd,该服务将在关闭时启动,并以某种方式通知系统正在关闭.

If possible, I'd like to create a shell script that will be provided to launchd as an on-demand service that will be started near to shutdown and somehow informed that the system is shutting down.

或者,我可能需要一个shell脚本,该脚本在登录/系统启动时打开并保持运行(或者更是处于睡眠状态),直到收到SIGTERM或其他kill信号为止,以便它可以在关闭之前运行一些命令.

Alternatively, I may need a shell script that is opened on login/system start and remains running (or rather, asleep) until a SIGTERM or other kill signal is received, so that it can run some commands before closing.

推荐答案

@gaige感谢您的答复!最后,我去了以下方面:

@gaige thanks for the reply! In the end I went for the following:

#!/bin/sh
StartService() {
    echo "Started"
}

StopService() {
    echo "Stopped"
    exit 0
}

StartService
trap StopService SIGTERM
while true; do
    sleep 86400 &
    wait $!
done

睡一整天应该可以避免浪费任何处理能力,尽管运行这样的东西并不是我最喜欢的解决方案.但是,launchd确实没有任何标准,似乎它只能允许我的脚本在关机时运行.上面的代码在负载(登录)上运行,然后在终止,注销或关闭时捕获SIGTERM.请注意sleep的异步使用,因为某些环境在执行陷阱之前将等待睡眠完成,这没有好处,因为launchd仅允许在发送SIGKILL之前几秒钟响应SIGTERM

Sleeping for a full day should prevent it from wasting any processing power, though it's not exactly my favourite solution to have something running like that. However, launchd doesn't really have any criteria that seemed like it would allow my script to only run at shutdown. The above instead is run on load (login) and then captures a SIGTERM on termination, logout or shutdown. Note the asynchronous use of sleep, as some environments will wait for the sleep to finish before executing the trap, which is no good as launchd only allows a couple of seconds to respond to SIGTERM before SIGKILL is sent.

据我所知launchd仅支持按需服务,如果它们是由套接字或监视的路径或其他任何方式触发的,那对我来说并没有真正的解决方案,因为它仍然需要某种外部环境影响力.

As far as I can tell launchd only supports on demand services if they're triggered by a socket or watched path or whatever, which didn't really present a solution for me since it would still require some kind of outside influence.

但是,一个有益的副作用是,我在StartService中获得的任何结果(例如文件指针等)都可以由Stop​​Service轻松访问,因为它仅位于内存中,而无需保存文件.只是感觉不是最优雅的解决方案,但是,嘿,如果可行,那就很好了!

One beneficial side effect however is that any results I obtained in StartService, such as file pointers etc., can be easily accessed by StopService since it's just in memory, no need for file saving. Just doesn't feel like the most elegant solution, but hey, if it works it's fine!

这篇关于通过启动在关机时运行Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆