【问题标题】:Crontab not starting command on rebootcrontab 在重新启动时未启动命令
【发布时间】:2014-10-13 04:31:05
【问题描述】:

我需要一个在重启时运行的命令。这个命令叫做 script.sh

#!/bin/bash
ibeacon scan | /home/pi/test.py

这个命令将输入​​传递给我编写的名为 test.py 的 python 程序。

#!/usr/bin/python
import fileinput
import socket
import requests

for line in fileinput.input():
   if line.startswith('UUID:'):
      line = line.strip()
      a = line.split(' ')[1]
      b = line.split(' ')[3]
      c = line.split(' ')[5]
      d = line.split(' ')[7]
      e = line.split(' ')[9]
      f = socket.gethostname()
      payload = {'uuid': a, 'major': b, 'minor': c, 'power': d, 'rssi': e, 'hubname': f}
      r = requests.get('http://httpbin.org/get', params=payload)
     # print(r.url)     ##Print the URL
   elif line.startswith('iBeacon'):
      print "Starting script..."
      continue
   else:
      print "Error: invalid input, closing..."
      break

问题是当我检查进程时程序没有运行。系统日志文件指出它在重新启动时启动了命令,但它没有运行。

Aug 19 22:23:35 raspberrypibeacon /USR/SBIN/CRON[2089]: (root) CMD (/home/pi/script.sh > /dev/null 2>&1)

我的 crontab 条目如下所示

@reboot /home/pi/script.sh > /dev/null 2>&1

是 cron 的问题还是我的程序的问题?我的程序在命令行运行时运行良好,但在由 cron 执行时运行良好。

【问题讨论】:

  • 尝试将输出重定向到文件而不是 /dev/null/ 以确保它正在运行。它还可能为您提供有价值的调试信息
  • 使用 root 权限运行东西是个很糟糕的主意

标签: linux python-2.7 cron raspberry-pi crontab


【解决方案1】:

通常,如果某些东西在命令行中运行良好但不是作为 cron 作业,那是因为 cron 作业的 PATH 设置不同。 PATH 设置得相当保守,以确保不会运行错误的东西,这会导致安全问题。

您可以首先将 ibeacon 程序的完整路径放在 shell 脚本中(即,将其运行为

/path/to/ibeacon scan | ...

而不仅仅是

ibeacon scan | ...

该脚本也以 root 身份运行,因此如果 ibeacon 位于您自己的 bin 目录中,则如果没有完整路径,将无法找到它。

【讨论】:

    猜你喜欢
    • 2016-10-04
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2015-02-06
    • 2015-03-06
    • 2014-03-25
    相关资源
    最近更新 更多