Skip to content
Advertisement

Crontab not starting command on reboot

I need a command to run at reboot. This command is called script.sh

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

This command pipes input to a python program I wrote called test.py.

#!/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

The problem is the program is not running when I check the processes.The syslog file states that it started the command on reboot, but it is not running.

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

my crontab entry looks like this

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

Is the problem with cron or with my program? My program works fine when run from the command line, but not when executed by cron.

Advertisement

Answer

Quite often, if something runs fine from the command line but not as a cron job, it’s because the PATH is set differently for cron jobs. The PATH is set quite conservatively to make sure that the wrong thing doesn’t get run, which would cause security issues.

You could start by putting the full path to the ibeacon program in the shell script (i.e., run it as

/path/to/ibeacon scan | ...

rather than just

ibeacon scan | ...

The script is also being run as root, so if ibeacon is in your own bin directory, it won’t be found without the full path.

Advertisement