Search code examples
pythonpython-2.7ubuntucronubuntu-18.04

Program execution with crontab


Hardware setup (computer, etc)

  • Ubuntu server 18.04.1
  • Serial To Usb Converter with 8 ports

Python version

  • 2.7.15r1

Python program description

When the program starts to create some threads:

  1. Create one thread for the Modbus server.
  2. Run 1 thread for each serial port connected (/dev/ttyUSBn) and start read the data.

Problem explanation

When I run the script using the normal command (python2.7 myProgram.py) it work, so the modbus server start and I can read the value, and I can also see the usb-serial convert blink on the TX-RX leds. If I check the readed data they are correct, so the program is working properly.

The problem come out when I set up a crontab job that run my python script!

The modbus server start properly, but I can't see the usb-serial converter leds blink and the python program don't print the readed data. That means the program is not working on the "serial" side.

To create the job I has used the following commands:

  1. crontab -e
  2. selected nano (default option)
  3. added at the end of the file the cron command: @reboot /usr/bin/python2.7 /myProgram.py

I can't figure out where the problem is, the program is not catching the exception and the process is still running until I stop it manually. If I stop it and run it manually after that it start and work properly.

To help you:

I have also tried to run it using **systemctl**, the problem is the same. At boot the service start and if I check it I can read: Active(running), but the software is not reading from the serial port.

The questions are:

  • How can I solve it?
  • There is something wrong with the crontab job?
  • Maybe crontab job can't access the /dev/ directory? How can I solve this?

I'm very confused about that, I hope the question is properly created and formatted.


EDIT 30/11/18:


I have removed the crontab command, and create a service to run the program using this procedure.

If I run the command: service supervision start I can see that the process is running correctly and on htop I have only 4 processes.

htop output

service status output

In this case, the program is not reading from the serial port, but the modbus server is working. You can see that I have just 4 processes and the cpu load is too high.

If I run it manually with the command: python2.7 LibSupervisione.py

The output of the htop command is: outupt of working program

Here you can see that I have more processes, 1 for each thread that I create and the load on the cpu is properly distributed.


Solution

  • Your script probably requires a Console or some Environment variables, but in a systemd started process you dont have these automatically.

    The easiest way would be to prepend /usr/bin/bash -c "your command" in your System unit in the field ExecStart to enable a Shell like Environment likle this:

    ExecStart=/bin/bash -c "/usr/bin/python2.7 /myProgram.py" 
    
    WorkingDirectory=yourWorkingDir 
    

    Why do you Need to use cron? Use a systemd timer instead.