简体   繁体   中英

Automating a Python script with Crontab

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM