Jump to content

Request Tutorial To Run Python Script As Cronjob


Recommended Posts

Posted

I need a step by step tutorial on how to run a Python script as cron job with all the things that have to be done in preparation and all the paths with a example script. I'd like the script to work in a directory different than the public one if possible. /home/heirloom/database/main.py.The first line of the python file is #!/usr/bin/env python. Is it correct? And the permissions 755. Where do I see that I actually have python in johnny? Also when I write the cron job for a specific hour the time would be UTC?

 

Try to use this script as an example so I can see if the script actually worked:

 

/home/username/hello.py

#!/usr/bin/python
with open('hello.text','w+') as f:
    f.write('Hello server!')
cron job to execute the script at 9:15 UTC. It didn't work in my case, I created it at 9:13.

15 9 * * *  cd /home/username ; /usr/bin/python hello.py
Do I need a cron job to run the script? I've read perl scripts can be executed by clicking on them.

In this web http://www.heliohost.org/classic/features/languages/python what does 'output a Content-type header before anything else' mean. I'm not creating a web with python I just want to print 'Hello' in a file. I've found a tutorial that explains how to run scripts via cron jobs.

 

I followed this tutorial which is exactly the kind of tutorial that there should be in the heliohost wiki. But everything worked fine till the part when it explains how to execute the script in another directory. The problem with executing my script in a public directory is that anyone could be updating the database and I only want to update it once a month because of the overhead. In the tutorial it says:

 

If you would like to run the Python file not in the cgi-bin folder (in public_html or any other directory), it is necessary to add the following code to the .htaccess file in the same directory where the Python script is placed:

 

Options +ExecCGI

AddHandler cgi-script .py

But it doesn't say how to do that. And cPanel doesn't allow me to choose the directory in which to add the handler. Here it says that I have to add the following to httpd.conf (in my case would be):

<Directory /home/heirloom/database />
  Options +ExecCGI
  AddHandler cgi-script .py
  Order allow,deny
  Allow from all
</Directory>
But I can't change that file so if an admin could add that to the httpd.conf file. I'd also need around 10 cron jobs a day for testing and when I know how to use it I'm good with one every month.

Server: johnny

User: heirloom

Posted

I'd like the script to work in a directory different than the public one if possible. /home/heirloom/database/main.py.

CGI have to be in public_html or a subdirectory. If you're running it from cron it can be anywhere.

 

The first line of the python file is #!/usr/bin/env python. Is it correct?

If you use that shebang on Johnny you'll be using a truely ancient python version 2.6.6 that was released like 8 years ago. Do you really want to do that? If you use /usr/bin/python2.7 or /usr/bin/python3.6 you'll be using very new, very up to date versions.

 

Where do I see that I actually have python in johnny?

All accounts on all servers have python.

 

Also when I write the cron job for a specific hour the time would be UTC?

No, cron currently run on pacific US time because that's where the servers are located.

 

cron job to execute the script at 9:15 UTC. It didn't work in my case, I created it at 9:13.

Check this link to see what time it is in California. https://www.google.com/search?q=what+time+is+it+currently+in+california

 

Do I need a cron job to run the script?

No, you can run python as a cron, as cgi, as django, or as flask. If you want to use django or flask you'll need to switch servers though because Johnny doesn't support those.

 

I've read perl scripts can be executed by clicking on them.

There's no way to "click" on a script without a gui. You can do that on your home computer though.

 

If you would like to run the Python file not in the cgi-bin folder (in public_html or any other directory), it is necessary to add the following code to the .htaccess

You're getting cgi and cron python mixed up. If you want to run your python script as a cgi in public_html then that code in .htaccess is necessary. It's not needed for what you're trying to do.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...