内似系统自带的那些“服务”,他们在关机前,会先执行停止服务的脚本,现在我自己写的普通脚本,希望在关机或重启时被执行,请教怎么办?做个简单的比喻:我要在关机/重启时执行一条命令“date”,怎么办?

解决方案 »

  1.   

    errrr,这个貌似不是很复杂啊?init.rc5(大概是这个目录)里看看就知道了。
      

  2.   

    到Linux版去问比较好 http://forum.csdn.net/BList/Linux
    展开左边“更多论坛”
      

  3.   

    启动的话,可以放到/etc/rc.local里
    关机的话,看这个 http://blog.chinaunix.net/u/31550/showart_315950.html
      

  4.   

    谢了啊,一直没注意到这个地方,哈哈嗯,是这么个意思,但我一直没写成功过,能不能就以,开机/关机时都能执行"date"这个命令写一个示范脚本啊?
      

  5.   

    无论是关机还是重启,或者是普通的启动,
    不过是在不同的runlevel之间切换罢了Runlevels S, 0, 1, and 6 are reserved.  
    Runlevel S is used to initialize the system on  boot. When starting  
    runlevel  S  (on  boot) or runlevel 1 (switching from a multi-user runlevel) 
    the system is entering "single-user mode", after which the current runlevel is S.  Runlevel 0 is used  to  halt the system; 
    runlevel 6 is used to reboot the system.
    运行级别 0 是关机, 6 是重启。可以看发行版的manpages说明
    $ man 8 init不同发行版可能做法有所不同,Debian Linux是在/etc/rcX.d做手脚
    以cron为例,/etc/init.d/cron就是cron服务的脚本,把这个脚本做一个符号连接
    到/etc/rcX.d下,那么在切换到X运行级别的时候,就会以某个参数去调用它。
    做符号连接的时候,如果符号连接名字以S开头,那么就用start参数调用脚本,
    如果名字以K开头,那么就以stop参数调用脚本,K和S后面的数字,表示顺序,数字小的先执行。各个发行版都有类似的管理机制,表现可能有所不同,但是思路都差不多的。
      

  6.   

    #!/bin/sh
    # Start/stop the cron daemon.
    #
    ### BEGIN INIT INFO
    # Provides:          cron
    # Required-Start:    $remote_fs $syslog $time
    # Required-Stop:     $remote_fs $syslog $time
    # Default-Start:     2 3 4 5
    # Default-Stop:      1
    # Short-Description: Regular background program processing daemon
    # Description:       cron is a standard UNIX program that runs user-specified 
    #                    programs at periodic scheduled times. vixie cron adds a 
    #                    number of features to the basic UNIX cron, including better
    #                    security and more powerful configuration options.
    ### END INIT INFO
    test -f /usr/sbin/cron || exit 0PIDFILE=/var/run/crond.pid
    # In some systems the pidfile might be (incorrectly) set to /etc
    # if this pidfile is present, use it instead.
    [ -e /etc/cron.pid ] && PIDFILE=/etc/crond.pid
    [ -r /etc/default/cron ] && . /etc/default/cron. /lib/lsb/init-functions# Read the system's locale and set cron's locale. This locale
    # will be inherited by cron (used to set charset of emails)
    # and tasks running under it.parse_environment () 
    {
        ENV_FILE="none"
        [ -r /etc/environment ] && ENV_FILE="/etc/environment"
        [ -r /etc/default/locale ] && ENV_FILE="/etc/default/locale"
        [ $ENV_FILE = none ] && return    for var in LANG LC_ALL LC_CTYPE; do
            value=$(egrep "^[^#]*${var}=" $ENV_FILE | tail -n1 | cut -d= -f2)
            eval $var=$value
        done
    }# Parse the system's environment
    if [ "$READ_ENV" = "yes" ] ; then
        export LANG LC_ALL LC_CTYPE
        parse_environment
    fi
    case "$1" in
    start) log_daemon_msg "Starting periodic command scheduler" "cron"
            start-stop-daemon --start --quiet --pidfile $PIDFILE --name cron --startas /usr/sbin/cron -- $LSBNAMES $EXTRA_OPTS
            log_end_msg $?
    ;;
    stop) log_daemon_msg "Stopping periodic command scheduler" "cron"
            start-stop-daemon --stop --quiet --pidfile $PIDFILE --name cron
            log_end_msg $?
            ;;
    restart) log_daemon_msg "Restarting periodic command scheduler" "cron" 
            start-stop-daemon --stop --retry 5 --quiet --pidfile $PIDFILE --name cron
            start-stop-daemon --start --quiet --pidfile $PIDFILE --name cron --startas /usr/sbin/cron -- $LSBNAMES $EXTRA_OPTS
            log_end_msg $?
            ;;
    reload|force-reload) log_daemon_msg "Reloading configuration files for periodic command scheduler" "cron"
    # cron reloads automatically
            log_end_msg 0
            ;;
    status)
           log_action_begin_msg "Checking periodic command scheduler"
           if pidofproc -p "$PIDFILE" >/dev/null; then
                log_action_end_msg 0 "running"
                exit 0
           else
               if [ -e "$PIDFILE" ]; then
                    log_action_end_msg 1 "failed to start"
                    exit 1
               else
                    log_action_end_msg 0 "not running"
                    exit 3
               fi
           fi
             ;;
    *) log_action_msg "Usage: /etc/init.d/cron {start|stop|status|restart|reload|force-reload}"
            exit 2
            ;;
    esac
    exit 0
    这个拿去改改就可以了,5分钟应该够了吧