启动时运行脚本困难

启动时运行脚本困难

我正在运行 Ubuntu 16.04,并连接了 USB CEC 适配器来控制我的电视。我使用的是修改版的这个脚本启动一个cec-server接受并发送命令的程序。

因此,脚本可以正常工作;只要我在启动后手动运行它。当我发出时pidof cec-client,它会给我一个 ID,我可以向发送命令,这些命令cec-server会立即生效。这样一切都很棒。

但是,我似乎无法通过 crontab 或启动应用程序使其工作。pidof cec-client不会返回 ID 并且我无法发送命令。

TL;DR:当我手动运行脚本时,它有效。通过 crontab 不起作用。希望在重启后自动运行它。

编辑:

我使用的 crontabline 是@reboot /home/server/Documents/cecserver.sh

脚本本身:

#!/bin/bash
#
# Set up a fifo and connect cec-client to it
#
# Original By Will Cooke.  http://www.whizzy.org

CECLOG="/home/server/Documents/logs/cec.log"
CECDEV="/dev/ttyACM0"
CECFIFO="/home/server/Documents/logs/cec.fifo"
CECCLIENT="/usr/bin/cec-client -d 1 -p 1 -o Codex"

timestamp(){
 date +"%Y-%m-%d %T"
}

log(){
    echo "SERVER $(timestamp):  $1" >> $CECLOG
}

echo "SERVER $(timestamp): File loaded ($1)" >> $CECLOG

log "Starting server.  Since only one server can run at a time, stopping first."
log "Begin shutting down..."
declare -a TAILPIDS=(`ps aux | grep 'tail -f /dev/null' | egrep -v grep | awk '{print $2}'`)
declare -a CATPIDS=(`ps aux | grep 'cat $CECFIFO' | egrep -v grep | awk '{print $2}'`)
if [ ${#TAILPIDS[@]} -gt 0 ]
  then
    log "Found some tail processes..."
    for i in "${TAILPIDS[@]}"
      do
        log "Killing $i"
        kill $i
      done
fi

if [ ${#CATPIDS[@]} -gt 0 ]
  then
      log "Found some cat processes..."
      for i in "${CATPIDS[@]}"
        do
      log "Killing $i"
      kill $i
    done
fi

log "Asking cec-client to stop if it's running..."
killall -s 2 cec-client >> $CECLOG
log "Trying to remove FIFO..."
rm $CECFIFO >> $CECLOG
log "Done shutting down."
log "Done stopping, now starting..."
log "Setting up FIFOs..."
mkfifo $CECFIFO
log "Open pipe for writing..."
tail -f /dev/null > $CECFIFO &
log "Opening pipe for reading and start cec-client..."
(cat $CECFIFO | $CECCLIENT &) > /dev/null
log "Start up complete."

exit 0

相关内容