graph-cli icon indicating copy to clipboard operation
graph-cli copied to clipboard

Graph not working when run in script via cronjob ?

Open nodecentral opened this issue 4 years ago • 19 comments

Hi

I have a Lua script that I’m trying to run via crontab below.

# m h  dom mon dow   command
0 8 * * * /usr/bin/lua /home/pi/shared/TS_cabin_graph_watts_12h.lua >/home/pi/shared/watts.log 2>&1
1 8 * * * /usr/bin/lua /home/pi/shared/TS_cabin_graph_temp_12h.lua >/home/pi/shared/temp.log 2>&1 

The whole script does a lot of different things, but here’s the specific os.execute graph request below.

print("Start : graph command")
local x = os.execute('graph /home/pi/shared/feed12hr.csv -y 9,10 --ylabel Temp --title CabinTemp --figsize 1600x1000 --output /home/pi/shared/cabingraph12hr.png') 
print(x)
print("End : graph command") 

When I run it manually at the command line it works fine - see output below.

Start : graph command
/usr/local/lib/python2.7/dist-packages/pandas/plotting/_converter.py:129: FutureWarning: Using an implicitly registered datetime converter for a matplotlib plotting method. The converter was registered by pandas on import. Future versions of pandas will require you to explicitly register matplotlib converters.

To register the converters:
        >>> from pandas.plotting import register_matplotlib_converters
        >>> register_matplotlib_converters()
  warnings.warn(msg, FutureWarning)
0
End : graph command

but when I check the logs after running it via a cronjob, it fails and reports the following

Part 4
Start : graph command
32512
End : graph command

Any idea of the cause and required fix ?

nodecentral avatar Mar 31 '21 09:03 nodecentral

Hm, so when you execute the lua script manually it works, but executing the lua script from cron it does not?

My first thought is a permissions issue.

mcastorina avatar Mar 31 '21 16:03 mcastorina

Yes, exactly it’s very strange

I was thinking it could be something like permissions, but I have no idea how that works with cron ? Any ideas/suggestions?

nodecentral avatar Mar 31 '21 18:03 nodecentral

I found your SO post: https://stackoverflow.com/questions/66885493/lua-error-running-script-via-crontab-with-os-execute-command-exit-code-325

Did you try the suggested answer? Seems plausible to me that cron has a minimal PATH

mcastorina avatar Mar 31 '21 18:03 mcastorina

Thanks - I had not noticed someone had responsed..

Using ‘whereis graph’, it returns the following.

graph: /usr/local/bin/graph

Which suggests I would need to update the graph part of my Lua code to this, I’ll give it a go..

print("Start : graph command")
local x = os.execute('/usr/local/bin/graph /home/pi/shared/feed12hr.csv -y 9,10 --ylabel Temp --title CabinTemp --figsize 1600x1000 --output /home/pi/shared/cabingraph12hr.png') 
print(x)
print("End : graph command") 

nodecentral avatar Mar 31 '21 19:03 nodecentral

Well I tried the full path, and while it still works when I run it manually (as pi user) - it sadly still doesn’t work via crontab, although this time I now receive a 256 error/exit code, which at least suggests something has changed,

Also rather than call it with os.execute(), I also tried it with io.popen , which is another option with Lua, but that gave me a very specific graph error - any ideas ?.

Start : graph command
Traceback (most recent call last):
  File "/usr/local/bin/graph", line 11, in <module>
    sys.exit(main.main())
  File "/usr/local/lib/python2.7/dist-packages/graph_cli/main.py", line 10, in main
    graphs = get_graph_defs(args)
  File "/usr/local/lib/python2.7/dist-packages/graph_cli/graph.py", line 171, in get_graph_defs
    graphs, globals = read_chain(args)
  File "/usr/local/lib/python2.7/dist-packages/graph_cli/graph.py", line 194, in read_chain
    data = stdin.buffer.read()
AttributeError: 'file' object has no attribute 'buffer'

End : graph command

which ties back to this one (I think) - https://github.com/mcastorina/graph-cli/issues/39

nodecentral avatar Apr 01 '21 08:04 nodecentral

Correct, that AttrbitueError is a python2 issue. I'll have a fix for it today

mcastorina avatar Apr 01 '21 17:04 mcastorina

v0.1.11 should fix the attribute error now.

mcastorina avatar Apr 01 '21 18:04 mcastorina

Great many thanks - How best do I update my Pi with the latest version ?

nodecentral avatar Apr 01 '21 21:04 nodecentral

pip install graph-cli --upgrade

Assuming you used pip originally. If you used pip2 then upgrade with pip2

mcastorina avatar Apr 01 '21 21:04 mcastorina

Assuming you used pip originally. If you used pip2 then upgrade with pip2

Great many thanks - How can I find out which version of pip I used originally?

nodecentral avatar Apr 03 '21 13:04 nodecentral

You can see what was installed with pip using pip freeze

pip freeze | grep graph-cli

That will also tell you the version installed.

mcastorina avatar Apr 03 '21 17:04 mcastorina

Great, ok, I’ve done that, and this is what it returned..

pi@raspberrypi:~ $ pip freeze | grep graph-cli
graph-cli==0.1.7

How do I work out the pip version ?

nodecentral avatar Apr 03 '21 18:04 nodecentral

That means it was installed with pip, so you can go ahead and do

pip install graph-cli --upgrade

mcastorina avatar Apr 03 '21 19:04 mcastorina

Thanks - not sure if you can help with this, but for some reason every attempt I’ve made to do the upgrade it fails on my raspberry pi - it gets part way through, but then I’m exited out of the terminal and it returns the following.

Connection closed with error: connection reset by peer

nodecentral avatar Apr 04 '21 08:04 nodecentral

Sounds like a network issue.. I can't help you there 😕

mcastorina avatar Apr 04 '21 16:04 mcastorina

I’ve google’d the hell out of this and no luck fixing it. - so I’m going to try and do a whole new Rasp Pi build and install graph_cli from scratch.

nodecentral avatar Apr 04 '21 17:04 nodecentral

Wow, I forgot how long it takes to install :-)

This part took ages, why is that ?

Building wheels for collected packages: pandas, subprocess32, kiwisolver
  Running setup.py bdist_wheel for pandas ...

nodecentral avatar Apr 04 '21 18:04 nodecentral

Hi, quick update, the Installation claimed to have been successful, but I now receive a new error whenever I run graph..

I’ve posted it as a new issue.. https://github.com/mcastorina/graph-cli/issues/47

nodecentral avatar Apr 04 '21 19:04 nodecentral

pandas is a pretty big data processing library that needs to be compiled (also relies on numpy). That's the reason for the long install time.

mcastorina avatar Apr 04 '21 20:04 mcastorina