0

I have a working python 2.7 program that reads a temperature sensor and vibration sensor from the GPIO ports . I can execute it from both the Python environment and the command line. The program is in /home/pi/Documents/KS_Filtration

The following is my entry in crontab

@reboot sudo python /home/pi/Documents/KS_Filtration/main_rev1.py

Permissions are set to 644.

I have searched answers on this site and haven't found anything that fixes my problems. Where am I going wrong? What should I expect to see when I reboot? I've checked the task manager and python is not running.

1

As the answer by @recantha notes, you might have a path issue. Jobs running under crontab get a different set of environment variables, including the variables that establish the path to executables. If that's the case for you here, then you fix it by replacing python with the fully qualified path to the executable, which you can find on the command line by typing which python.

If your python script also refers to any external programs, then you'll potentially need to fully qualify the paths to those too, inside of your script.

Another point is that you typically don't run sudo from your own crontab. If you want to run with privilege, then it's more usual to run the job from root's crontab. That means using sudo when you call crontab to define the job:

sudo crontab -e

and then inside the file:

@reboot <full path>/python /home/pi/Documents/KS_Filtration/main_rev1.py

The need for this is somewhat mitigated by the fact that you typically don't need a password to run sudo on the Pi, but it's good security practice on most systems. Related discussion here: https://askubuntu.com/questions/173924/how-to-run-a-cron-job-using-the-sudo-command

0

I think you need to provide a full path to python. Type the following to find out where Python is located:

which python

This should give you something like

/usr/local/bin/python

Change your command to something like

@reboot sudo /usr/local/bin/python /home/pi/Documents/KS_Filtration/main_rev1.py

This will run your Python programme 'headless'. i.e. it will run it as a process that you can't 're-attach' to.

Once you've rebooted, open up a Terminal and type the following:

ps -ef | grep python

This lists all the running processes and then filters it to look for Python. You should see your script running once you've got the paths sorted out.

If you want to capture the output from the script, you could try adding this to your cron task:

> /home/pi/Documents/KS_Filtration/log.log

Which should redirect your output to a log file. If this doesn't work, you'll need to add some logging to your program. Take a look here for help on file operations

  • Apparently, my script is running. It sends me emails during the day and I'm receiving those emails. The question that I still have is what should I see when it reboots? I looked at the task manager and there is no indication that my program is running. There is nothing on my monitor that indicates it is running. In my program, I'm doing print statements, what happens to those? – erhixon Aug 1 '17 at 19:57
  • I've updated my answer with some more information. Let me know if that does/doesn't help. – recantha Aug 2 '17 at 4:43
  • This helps. My problem was that I couldn't see it executing. It was executing, but I couldn't figure out how to see it running. – erhixon Aug 3 '17 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.