0

I am trying to automate a Python script on a Google Cloud VM using Crontab.

When I run python Daily_visits.py my code runs as expected and generates a table in BigQuery.

My Crontab job is as follows: */2 * * * * /usr/bin/python Daily_visits.py but generates no output.

I've run which python and I get /usr/bin/python back, and I have run chmod +x Daily_visits.sh on my file, what else am I missing?

1
  • if you run crontab -l do you visualise an entry regarding the cronjob you have just created? Commented Mar 20, 2018 at 14:56

2 Answers 2

4

You should write absolute path for Daily_visits.py file. Go to Daily_visits.py file's directory and run command:

pwd

You take output like it:

/var/www/Daily_visits.py

Copy and paste it change into crontab.

*/2 * * * * /usr/bin/python /var/www/Daily_visits.py

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you @Mert - I tried this but I am still getting no results - all the files are in the same directory so I assumed I did not need the full path. Any other suggestions?
@BenP ok, what did you take output pwd command? Can you show me?
pwd returns /home/bpowis so I changed my job to * * * * * /usr/bin/python /home/bpowis/Daily_visits.py
@BenP now, run this command -> /usr/bin/python /home/bpowis/Daily_visits.py and is it running?
@ Mert Yes this runs and generated the expected output.
|
1

You should have written something like

*/2 * * * * /usr/bin/python /path/Daily_visits.py

or you should have placed the script in the very same folder.

UPDATE

The status of the cron service was stopped since you are working on a Google Cloud Shell and not on a proper virtual machine.

As @MertSimsek told you it was enough to start the cron service and then everything works as expected.

$ sudo service cron start

Notice that since merely the home of the user is persistent all the change will be lost after you close it and open it again.

It is intended as a tool to run commands/small scripts not to develop and to be used as a normal virtual machine

Cloud Shell instances are provisioned on a per-user, per-session basis. The instance persists while your Cloud Shell session is active and terminates after a hour of inactivity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.