使用linux服务和python文件每12小时重启一次apache

时间:2013-04-28 08:28:38

标签: python linux apache bash

您好我想每12个小时使用它来重启apache服务 我将文件apache_rest放入/etc/init.d/apache_rest 我跑,但得到一些错误:

[root@localhost init.d]# service apache_rest start
Starting server
/etc/init.d/apache_rest: line 13: /sbin/start-stop-daemon: No such file or directory

apache_rest文件:

#!/bin/bash
WORK_DIR="/var/lib/foo"
DAEMON="/usr/bin/python"
ARGS="/home/lol/apache.py"
PIDFILE="/var/run/foo.pid"
USER="foo"
case "$1" in
  start)
    echo "Starting server"
    mkdir -p "$WORK_DIR"
    /sbin/start-stop-daemon --start --pidfile $PIDFILE \
        --user $USER --group $USER \
        -b --make-pidfile \
        --chuid $USER \
        --exec $DAEMON $ARGS
    ;;
  stop)
    echo "Stopping server"
    /sbin/start-stop-daemon --stop --pidfile $PIDFILE --verbose
    ;;
  *)
    echo "Usage: /etc/init.d/$USER {start|stop}"
    exit 1
    ;;
esac
exit 0

和python文件:

#!/usr/bin/python
import os,signal,time,multiprocessing
stop_event = multiprocessing.Event()
def stop(signum, frame):
    stop_event.set()
signal.signal(signal.SIGTERM, stop)
if __name__ == '__main__':
    while not stop_event.is_set():
        os.system('service httpd restart')
        time.sleep(43200)

谢谢^ _ ^

1 个答案:

答案 0 :(得分:3)

只需将此行添加到root的crontab:

0 1,13 * * * /etc/init.d/httpd restart

您不需要这样复杂的解决方案。