0

I am trying to run a python script in my Google VM Instance using Cron jobs.

  • My script is supposed to log some data from a website and stores it in a CSV file. I tried running it using the usual python3 kb_sc.py and it worked just fine.
    ... #scrape the website
    print("checkpoint")
    if not (os.path.isdir(path)):
        os.makedirs(path)
    if not (os.path.isfile(path + file)):
        data_new.to_csv(path+file, index = False)
    else:
        data = pd.read_csv(path+file)
        data = data.append(data_new)
        data.to_csv(path+file, index = False)
    print("done")
  • I enlisted it using sudo crontab -e and entered the scheduling as * * * * * cd /home/[myusername] && python3 kb_sc.py
  • I made sure my script was executable using sudo chmod +x kb_sc.py
  • I made sure CRON is running using sudo service cron status

enter image description here

  • I also even added > kb.log at the end of the CRON command * * * * * cd /home/[myusername] && python3 kb_sc.py to find out if it prints the checkpoint and done message. The kb.log file is created but it is empty.

Many other similar questions have been asked but it does not help, like 1, 2

I do not know what else is causing this issue, much less solving in.

user2552108
  • 1,107
  • 3
  • 15
  • 30
  • Specify the full path to the python .py file in `python3 kb_sc.py`. I would also specify the full path to `python3`. For CRON, do not assume PATH or environment exists. – John Hanley May 15 '19 at 00:20
  • Tried that as well, it didn't work – user2552108 May 15 '19 at 01:32
  • Then you should already have that in your screenshot as not specifying absolution paths is incorrect. If you have Stackdriver installed, you can see just about everything. If not, you can use the Serial Port. I highly recommend Stackdriver for tracing problems. – John Hanley May 15 '19 at 02:14
  • Hi, I was able to run a test .py file writing a csv, by using your crontab file. Logging worked as well. I'm testing on a Google VM instance too. So my guess is that something in your python code is hampering the scheduled run, even if it works in direct execution with `python3`. – alextru May 15 '19 at 10:53

0 Answers0