Jump to content

Debugging & Testing cron jobs


space2018

Recommended Posts

I'm trying to run PHP maintenance scripts on my site using cron in cPanel, which imposes a limit of 2 cron jobs per day (i.e. one every 24 hours), which is fine. Due to this limitation, how do I
go about debugging & testing cron jobs?

If I test-run the scripts, I will easily exceed the limit? It would be time consuming to wait 12 hours to test a script that previously failed due to a syntax error such as a wrong path or
directory.

Thanks in advance.

 

Link to comment
Share on other sites

As long as it's somewhere inside your public_html folder, you can just visit the script in your browser to trigger the script, and the output will appear on the page.

 

I personally would recommend setting a 

header("Content-Type: text/plain");

at the top of the script so the browser doesn't mangle the output for testing.

 

If it's not in public_html, you'll need to move it there, then do the above.

Link to comment
Share on other sites

MW actually has an extension specifically for shared hosts where there's no shell or where cron is restricted (like us).

 

You can use the extensions mentioned here to run them from a browser instead: https://www.mediawiki.org/wiki/Manual:Maintenance_scripts#No_shell_access and https://www.mediawiki.org/wiki/Extension:MaintenanceShell

Link to comment
Share on other sites

Thank you, I am aware of those extensions but they haven't been updated for years, are not officially supported by MediaWiki and cannot schedule tasks. My intention was to use cron to schedule the "maintenance" tasks on a daily, or weekly basis. But first, I wanted to run the scripts to make sure they actually worked.

 

It is my understanding that Python CGI scripts can execute shell commands (see: https://www.helionet.org/index/topic/29129-python-script-behavior/?hl=python) Could an admin please advise if this is a possible solution on Heliohost?

 

I greatly appreciate your response.

Link to comment
Share on other sites

I don't see any reason it wouldn't work, but I can't guarantee it. Python should have the ability to spawn processes under your user account though (unlike PHP...), so it's definitely something to explore. You'll need to adapt the scripts to fetch the output for you if you care about it, as the ones in that example only parse the output of ps to see if its already running. 

 

We have a user Bailey who is really good with MediaWiki too, perhaps he has an idea on this. He was working on rebuilding our Wiki a while back...

Link to comment
Share on other sites

  • 1 year later...

I had the same issue and couldn't debug the cron job to run a python script. at the end I created a python script which execute shell command then  I placed in a public folder with permission 755, you need to open it in a browser for the script to be executed.

the script is as follow:

#!/usr/bin/python3.7
 
print("Content-Type: text/html\n\n")
 
print("This will show if python is executing properly")
#replace /path/to/script.py with the path to your own script
 
#the below allow to execute shell script
import os
try:
    os.system('/usr/bin/env python3.7 /path/to/script.py')
    print("done")
except:
    print("failed")
Edited by jhm52
Link to comment
Share on other sites

I think it should be this though:

#the below allow to execute shell script
import os
try:
    os.system('/usr/bin/python3.7 /path/to/script.py')
    print("done")
except:
    print("failed")
Cuz the path to the Python compiler is /usr/bin/python3.7

 

Also, check your line endings in that line : print("Content-Type: text/html\n\n")

 

Open it using Notepad++ and go to Edit > EOL and check that it's selected to Unix. Otherwise, if it's Windows change it to Unix and the problems should be sorted.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...