我目前面临应该执行Python脚本的init.d脚本的问题。我已经确保了Python文件以及init脚本的权限。如果我用echo "test" > /tmp/test.txt
替换Python可执行行,那么Init脚本工作正常。
Python文件打印一行,然后循环播放1000次。
#!/bin/sh
#
# /etc/init.d/incapsula-loggrabber
# init script for Incapsula.
# generated by 'myself'.
#
# chkconfig: 345 93 63
# description: Incapsula Logging Script
#
. /etc/init.d/functions
NAME=incapsula-loggrabber
DESC="Incapsula Script to Push logs to Splunk"
PYTHON="/bin/python"
PIDFILE="/var/run/incapsula-loggrabber.pid"
PYTHON_FILE="/root/inittest.py"
do_start() {
echo Starting Incapsula Script...
echo $PIDFILE is the file where pid resides
sudo python $PYTHON_FILE &
RETVAL=`echo $?`
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$NAME
}
do_stop() {
echo $PIDFILE is removed
kill `cat $PIDFILE`
RETVAL=`echo $?`
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$NAME
}
do_status() {
if [ -e $PIDFILE ]; then
echo $NAME is running, pid=`cat $PIDFILE`
else
echo $NAME is NOT running
exit 1
fi
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
status)
do_status
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0
我有什么遗漏或需要进行任何修改才能使这个初始化脚本有效吗?
答案 0 :(得分:0)
问题解决了,我已经在反引号之间执行了python代码,以便先执行,然后将进程ID写入/ var / run下的文件,并将输出写入给定文件中python脚本。
echo `/bin/python /root/inittest.py ` & echo $! > $PIDFILE