简体   繁体   English

Linux start-stop-daemon目录错误调用shell / python脚本

[英]Linux start-stop-daemon directory error calling shell/python script

I am just getting acquainted with Linux and I cannot seem to get the start-stop-daemon to run a python script due to directory issues. 我只是熟悉Linux,由于目录问题,我似乎无法让start-stop-daemon运行python脚本。 In a linux file structure I have the files: 在linux文件结构中,我具有以下文件:

~/test.txt 〜/的test.txt

THIS LINE IS A TEST

~/test.py 〜/ test.py

#!/usr/bin/python
import time

with open("test.txt") as f:
    while True:
        try:
            print("Hello World")
            print(f.readline())
            time.sleep(2) 
        except KeyboardInterrupt:
            f.close()
            break

~/test.sh 〜/ test.sh

#!/bin/bash

echo "SHELL SCRIPT SUCCESS" > /var/log/test.log
cd ~/
./test.py > /var/log/test.log

Upon calling sudo bash ~/test.sh from any directory the test.log is populated as expected with the stdout originating from test.py . 从任何目录调用sudo bash ~/test.sh ,将按预期填充来自test.py的stdout来填充test.log For some reason, starting the following start-stop-daemon service script WILL generate a test.log but does NOT populate it with the stdout: 由于某些原因,启动以下start-stop-daemon服务脚本将生成一个test.log,但不会用stdout填充它:

/etc/init.d/test /etc/init.d/test

#!/bin/sh

### BEGIN INIT INFO
# Provides:     Python test script
# Required-Start:   $remote_fs $syslog
# Required-Stop:    $remote_fs $syslog
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    Prints out daemonized argument
# Description:      Creates output of argument
### END INIT INFO

DAEMON_DIR=/home/alex
DAEMON=$DAEMON_DIR/test.sh
DAEMON_NAME=test

DAEMON_OPTS="hello"
DAEMON_USER=root
PYTHON=/usr/bin/python

PIDFILE=/var/run/$DAEMON_NAME.pid

. /lib/lsb/init-functions

do_start () {
    log_daemon_msg "Starting system $DAEMON_NAME daemon"
    #start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --exec $PYTHON --startas $DAEMON 
    start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --chuid $DAEMON_USER --startas /bin/bash  /home/alex/test.sh
    log_end_msg $? 
}

do_stop () {
    log_daemon_msg "Stopping system $DAEMON_NAME daemon"
    start-stop-daemon --stop --pidfile $PIDFILE --retry 10
    log_end_msg $?
}

case "$1" in

    start|stop)
    do_${1}
    ;;

    restart|reload|force-reload)
    do_stop
    do_start
    ;;

    status)
    status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
    ;;

    *)
    echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
    exit 1
    ;;

esac
exit 0

Is this a directory issue that can be addressed within the start-stop-daemon ? 这是可以在start-stop-daemon解决的目录问题吗? Alternatively I'd be open to other methods of script servicing that can persist through a cold boot (ie no cron jobs) 或者,我愿意接受其他可以通过冷启动持续进行的脚本服务方法(即,没有cron作业)

Try calling cd using an absolute path, for example /home/alexjg/ instead of ~/ ; 尝试使用绝对路径调用cd ,例如/home/alexjg/而不是~/ the reason it was broken before is that in your example you're using sudo which keeps the home directory of the user running it. 之前被破坏的原因是在您的示例中,您使用的是sudo ,它使用户的主目录保持运行状态。 However when you're calling the bash script from init it will use root's home directory instead which doesn't contain test.py . 但是,当您从init调用bash脚本时,它将使用root的主目录代替,该目录不包含test.py

The file is created because the redirection is still succeeding; 创建该文件是因为重定向仍然成功; however because starting Python failed there was no output. 但是,因为启动Python失败,所以没有输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM