当进程花费超过10秒时,如何配置upstart以在关闭时运行脚本?
我在虚拟机(VirtualBox)中运行ubuntu 11.10以了解有关linux开发的更多信息。 我正在使用git存储库来保存我的工作,并编写了一个脚本来捆绑我的工作并将其保存到共享文件夹,以便在虚拟机未运行时使用。
我想在关机之前自动运行这个脚本,以便在vm关闭时我的工作始终可用(目前我必须手动运行脚本)。
我不知道upstart是否是实现此目的的最佳方式,但这是我作为测试编写的配置:
description "test script to run at shutdown" start on runlevel [056] task script touch /media/sf_LinuxEducation/start sleep 15 touch /media/sf_LinuxEducation/start-long end script pre-start script touch /media/sf_LinuxEducation/pre-start sleep 15 touch /media/sf_LinuxEducation/pre-start-long end script post-start script touch /media/sf_LinuxEducation/post-start sleep 15 touch /media/sf_LinuxEducation/post-start-long end script pre-stop script touch /media/sf_LinuxEducation/pre-stop sleep 15 touch /media/sf_LinuxEducation/pre-stop-long end script post-stop script touch /media/sf_LinuxEducation/post-stop sleep 15 touch /media/sf_LinuxEducation/post-stop-long end script
结果是仅完成一次触摸(预启动中的第一次触摸)。 我需要改变什么才能看到睡眠后的其中一个触摸工作? 或者有更简单的方法来实现这一目标吗?
提前致谢。
Upstart Intro,Cookbook和Best Practices有大量的代码片段可用于创建新手任务和作业。
cookbook的关闭过程部分说将运行/etc/init.d/rc
并调用/etc/init.d/rc
。 反过来,这将最终调用/etc/init.d/sendsigs
。 因此,如果您start on starting rc
那么您的任务将在rc之前执行(以及通常会关闭进程的sigterms)。
file:/etc/init/test.conf
description "test script to run at shutdown" start on starting rc task exec /etc/init/test.sh
文件:/etc/init/test.sh
touch /media/sf_LinuxEducation/start sleep 15 touch /media/sf_LinuxEducation/start-long
我认为这不能通过upstart来完成,因为/etc/init.d/sendsigs脚本在停止/重启时由upstart调用,在10秒内杀死所有进程( killall5 -9
),即使这不成功,它用于卸载所有东西并关闭。
最好的方法是使用生锈的/etc/init.d样式脚本。
示例: /etc/init.d/shutdown_job
#! /bin/sh ### BEGIN INIT INFO # Provides: shutdown_job # Required-Start: # Required-Stop: sendsigs # Default-Start: # Default-Stop: 0 6 # Short-Description: bla # Description: ### END INIT INFO PATH=/sbin:/usr/sbin:/bin:/usr/bin . /lib/lsb/init-functions do_stop () { date > /root/s.log sleep 20 date >> /root/s.log } case "$1" in start) # No-op ;; restart|reload|force-reload) echo "Error: argument '$1' not supported" >&2 exit 3 ;; stop) do_stop ;; *) echo "Usage: $0 start|stop" >&2 exit 3 ;; esac :
然后激活脚本
sudo update-rc.d shutdown_job start 19 0 6 .
这将把脚本放在运行级别0 a 6上的sentigs脚本之前(shutdown,reboot)。 此示例脚本将记录日期,然后hibernate20秒,然后再次将日期记录到/root/s.log 。)
更多信息: